python能不能进行异步协程编程?
网友回复
参考这个:python实现asyncio异步I/O协程操作代码
https://code.bfw.wiki/code/16881168521303040066.html
import asyncio async def fetch_data(url): print(f"Fetching data from {url}") await asyncio.sleep(2) print(f"Data received from {url}") async def main(): tasks = [ asyncio.create_task(fetch_data("https://api.example.com/data1")), asyncio.create_task(fetch_data("https://api.example.com/data2")), asyncio.create_task(fetch_data("https://api.example.com/data3")), ] await asyncio.gather(*tasks) asyncio.run(main())