Python 3.x 协程从来没有被等待过,异步多线程
我还是一个python初学者,我真的不知道应该在这里修改什么才能得到我想要的结果。Python 3.x 协程从来没有被等待过,异步多线程,python-3.x,python-asyncio,python-multithreading,aiohttp,Python 3.x,Python Asyncio,Python Multithreading,Aiohttp,我还是一个python初学者,我真的不知道应该在这里修改什么才能得到我想要的结果。 非常感谢您的帮助,如果这是一个重复的问题,我很抱歉。由于existance是异步的,您根本不需要线程,因此您可以像下面这样实现进程: RuntimeWarning: coroutine 'existance' was never awaited future = asyncio.ensure_future(process(names)) async def process(names): async
非常感谢您的帮助,如果这是一个重复的问题,我很抱歉。由于
existance
是异步的,您根本不需要线程,因此您可以像下面这样实现进程
:
RuntimeWarning: coroutine 'existance' was never awaited
future = asyncio.ensure_future(process(names))
async def process(names):
async with aiohttp.ClientSession() as s:
tasks = []
for name in names:
if len(name) >= 5 and len(name) < 16 and name.isalnum():
tasks.append(existance(s, name))
return await asyncio.gather(*tasks)
但是不要试图通过run\u-in\u-executor
调用异步函数,这是行不通的。不需要协同路由,它需要正常的函数。您试图以一种不起作用的方式混合使用多线程和异步IO。您应该使用线程池来执行阻塞IO,或者使用异步IO和协同路由。
async def process(names):
async with aiohttp.ClientSession() as s:
tasks = []
for name in names:
if len(name) >= 5 and len(name) < 16 and name.isalnum():
tasks.append(existance(s, name))
return await asyncio.gather(*tasks)
# instead of:
# result = slow_calculation(arg1, arg2) # sync/slow code
# use this:
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, slow_calculation, arg1, arg2)