python-3.x - 从未等待协程,异步多线程
问题描述
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 初学者,我真的不知道我应该在这里改变什么以获得我想要的结果。任何帮助表示赞赏,如果这是一个重复的问题,我很抱歉。
解决方案
由于existance
是异步的,你根本不需要线程,所以你可以这样实现process
:
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)
如果existance
正在调用一些可能会阻塞事件循环的同步慢速函数(例如 BeautifulSoup 解析),您可以通过仅通过线程池运行该函数来解决该问题。run_in_executor
像这样在本地引入:
# 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)
但是不要试图通过 调用异步函数run_in_executor
,那是行不通的。
推荐阅读
- go - 如果一个切片改变,两个切片都会改变
- c# - F1 2019 UDP解码
- python - 表 services_parceiros 没有名为 user_id, DJANGO 的列
- graphql - Pubsub 发布多个事件 Apollo Server
- swift - 将分钟转换为 HH:MM:SS - Swift
- bash - 使用 bash 遍历文件无法正常工作
- javascript - 如何更改范围滑块以显示金钱?
- android - 在 Fragment 中获取 ConstraintLayout
- python - 使用 callproc 从 Python 调用带有输出参数的 Teradata 存储过程
- node.js - 在猫鼬中按类别查找()博客不使用 NodeJs