如何使用 asyncio 避免来自事件使用者线程的 busy_wait?我有一个主线程,它生成由其他线程处理的事件。我的事件线程有busy_wait,因为它试图查看事件队列中是否有一些项目......
from Queue import Queue
from threading import Thread
import threading
def do_work(p):
print("print p - %s %s" % (p, threading.current_thread()))
def worker():
print("starting %s" % threading.current_thread())
while True: # <------------ busy wait
item = q.get()
do_work(item)
time.sleep(1)
q.task_done()
q = Queue()
t = Thread(target=worker)
t.daemon = True
t.start()
for item in range(20):
q.put(item)
q.join() # block until all tasks are done
如何使用 asyncio 实现类似于上述代码的功能?