如何在多线程环境中运行Python代码?
我正在尝试使用asyncio在Python上并行执行代码。其思想是并行运行多个作业 这是我的密码:如何在多线程环境中运行Python代码?,python,python-asyncio,Python,Python Asyncio,我正在尝试使用asyncio在Python上并行执行代码。其思想是并行运行多个作业 这是我的密码: import asyncio import threading async def print_thread(): for n in range(5): print("Number: {}".format(threading.get_ident())) if __name__ == '__main__': loop = asyncio.get
import asyncio
import threading
async def print_thread():
for n in range(5):
print("Number: {}".format(threading.get_ident()))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(print_thread())
finally:
loop.close()
输出为:
Number: 4599266752
Number: 4599266752
Number: 4599266752
Number: 4599266752
Number: 4599266752
据我所知,代码是在单个线程上执行的。有没有办法将其并行化
PS
如果我将代码更改为:
async def print_thread():
print("Number: {}".format(threading.get_ident()))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
for n in range(5):
loop.run_until_complete(print_thread())
我得到了相同的结果。您的
for
循环在您的协同程序中,因此它不能在不同的线程中。但即使将循环置于异步函数之外,它仍将在同一线程中运行:
import asyncio
import threading
async def print_thread():
print("Thread: {}".format(threading.get_ident()))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
tasks = []
for i in range(10):
tasks.append(asyncio.ensure_future(print_thread()))
loop.run_until_complete(asyncio.gather(*tasks))
仍将输出相同的id:
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
Thread: 140366741292864
解决方案是使用ThreadPoolExecutor
,但它需要一个函数,而不是一个协程,因此您必须从定义中删除async
:
import asyncio
import threading
import concurrent.futures
def print_thread():
print("Thread: {}".format(threading.get_ident()))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
with concurrent.futures.ThreadPoolExecutor() as pool:
for i in range(10):
loop.run_in_executor(pool, print_thread)
哪些产出:
Thread: 140446369556224
Thread: 140446361163520
Thread: 140446369556224
Thread: 140446361163520
Thread: 140446369556224
Thread: 140446352508672
Thread: 140446361163520
Thread: 140446344115968
Thread: 140446369556224
Thread: 140446335723264
正如您所看到的,线程少于调用,这是正常的。但是,如果批处理较大,可以使用ThreadPoolExecutor
构造函数中的max\u workers
参数更改线程计数
如果您仍然想使用协同程序,有一个解决方案:另一个答案和结果集合,如注释中所要求的:
import asyncio
import threading
import concurrent.futures
def get_thread():
return "Thread: {}".format(threading.get_ident())
if __name__ == '__main__':
loop = asyncio.get_event_loop()
with concurrent.futures.ThreadPoolExecutor() as pool:
tasks = []
for i in range(10):
tasks.append(loop.run_in_executor(pool, get_thread))
print(loop.run_until_complete(asyncio.gather(*tasks)))
输出:
['Thread: 139740266125056', 'Thread: 139740266125056', 'Thread: 139740266125056', 'Thread: 139740183525120', 'Thread: 139740266125056', 'Thread: 139740175132416', 'Thread: 139740183525120', 'Thread: 139740166739712', 'Thread: 139740266125056', 'Thread: 139740158347008']
不使用线程池(
multiprocessing.dummy.pool.pool
)的任何原因?asyncio
和multiprocessing
之间有区别吗?我是Python的新手。我怎么能等到你例子中的所有未来呢?我需要收集所有的期货并等待它们吗?是的,我会补充另一个答案!如果我需要添加一个锁来获取线程,是否可以在ThreadPoolExecutor中这样做?