Your two loops, for example, are completely CPU-bound and don't share any state, so the best performance would come from using ProcessPoolExecutor to run each loop in parallel across CPUs:import asynciofrom concurrent.futures import ProcessPoolExecutorprint('running async test')def say_boo(): i = 0 while True: print('...boo {0}'.format(i)) i += 1def say_baa(): i = 0 while True: print('...baa {0}'.format(i)) i += 1if __name__ == "__main__": executor = ProcessPoolExecutor(2) loop = asyncio.get_event_loop() boo = loop.run_in_executor(executor, say_boo) baa = loop.run_in_executor(executor, say_baa) loop.run_forever() 这篇关于如何使用 python 的 asyncio 模块正确创建和运行并发任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-29 05:36