【发布时间】:2020-10-21 14:15:49
【问题描述】:
async def existance(s, name):
async with s.head(f"https://example.com/{name}") as r1:
if r1.status == 404:
print('wow i worked')
async def process(names):
with ThreadPoolExecutor(max_workers=3) as executor:
async with aiohttp.ClientSession() as s:
loop = asyncio.get_event_loop()
tasks = []
for name in names:
if len(name) >= 5 and len(name) < 16 and name.isalnum():
task = loop.run_in_executor(
executor,
existance,
*(s, name)
)
tasks.append(task)
return await asyncio.gather(*tasks)
while True:
start_time = time.time()
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(process(names))
loop.run_until_complete(future)
我正在使用上面的代码尝试将我创建的任务拆分为多个线程,同时异步检查它们。
我收到此错误:
RuntimeWarning: coroutine 'existance' was never awaited
future = asyncio.ensure_future(process(names))
我还是一个 Python 初学者,我真的不知道我应该在这里改变什么以获得我想要的结果。 任何帮助表示赞赏,如果这是一个重复的问题,我很抱歉。
【问题讨论】:
-
run_in_executor不需要协程,它需要普通函数。您正在尝试以一种行不通的方式混合多线程和异步。你应该要么使用线程池来做阻塞 IO,要么使用 asyncio 和协程。
标签: python-3.x python-asyncio python-multithreading aiohttp