import asyncio
import threading
def create_event_loop_thread(workers,*args,**kwargs):
def _worker(*args,**kwargs):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(workers(*args,**kwargs))
finally:
loop.close()
return threading.Thread(target=_worker,args=args,kwargs=kwargs)
async def print_coro(*args,**kwargs):
print(f"巴拉巴拉{threading.get_ident()}:{args}:{kwargs}")
def start_thread(th):
[t.start() for t in th if isinstance(t,threading.Thread)]
def join_thread(th):
[t.join() for t in th if isinstance(t,threading.Thread)]
def main():
workers=[create_event_loop_thread(print_coro) for i in range(10)]
start_thread(workers)
join_thread(workers)
if __name__ == '__main__':
main()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)