python 携程asyncio实现高并发示例1
import asyncio
#携程(携程不是函数)
async def print_hello():
while True:
print("hello world")
await asyncio.sleep(1) #暂停1s
async def print_goodbye():
while True:
print("goodbye world")
await asyncio.sleep(2)
#创建携程对象
co1 = print_hello()
co2 = print_goodbye()
#获取事件循环
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(co1,co2)) #监听事件循环
hello world
goodbye world
hello world
goodbye world
hello world
hello world
goodbye world
hello world
hello world
hello world
goodbye world
多思考也是一种努力,做出正确的分析和选择,因为我们的时间和精力都有限,所以把时间花在更有价值的地方。

浙公网安备 33010602011771号