python - How to use thread in asyncio?
问题描述
I want to new a thread to do a io task with consumer/producer pattern in a asyncio task:
import asyncio
import threading
import time
import queue
q = queue.Queue()
def consumer():
while True:
v = q.get()
time.sleep(v)
print(f"log {v}")
async def work(v):
await asyncio.sleep(0.1)
print(f"work {v}")
async def main():
t = threading.Thread(target=consumer, daemon=True)
t.start()
for i in range(1, 5):
print(f"start {i}")
q.put(i)
loop.create_task(work(i))
print(f"done {i}")
t.join()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.run_forever()
But the asyncio task cannot be execute because of consumer thread.
解决方案
您可以使用内置在 asyncio 中的线程池(或根据需要创建自己的线程池)并调用run_in_executor
以向其提交任务,而不是显式管理工作线程和队列。例如:
import asyncio, time
def blocking_work(v):
# this function is not async, it will be run in a separate thread
time.sleep(v)
print(f"log {v}")
async def work(v):
# native asyncio work
await asyncio.sleep(0.1)
# followed by blocking IO wrapped in thread
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, blocking_work, v)
print(f"work {v}")
async def main():
await asyncio.gather(*[work(i) for i in range(1, 5)])
asyncio.run(main())
推荐阅读
- c++ - 如何修复“调用‘strtok’没有匹配的函数”
- xml - XmlEntries 到单个 CosmosDB 条目与 Azure 函数
- javascript - 异步时,arguments.callee 不返回可调用函数
- python - XGboost算法可以用于时间序列分析吗?
- php - 我在计算行数并在 php 中对它们进行排序时遇到问题
- c - 开发 C 编译此代码,但在文件创建期间给出 err 3221225477
- python - 熊猫 if else 条件多列
- c - 将单个值分配给多个变量的正确方法
- css - 我如何保持卡片图像 Bootstrap 4 的宽度响应
- ios - 如何使用 Alamofire 修补以下参数?