import time
def run(index):
print("lucky is a good man", index)
time.sleep(2)
print("lucky is a nice man", index)
for i in range(1, 5):
run(i)
运行结果:
lucky is a good man 1
lucky is a nice man 1
lucky is a good man 2
lucky is a nice man 2
lucky is a good man 3
lucky is a nice man 3
lucky is a good man 4
lucky is a nice man 4
import time
import asyncio
async def run(i):
print("lucky is a good man", i)
# 模拟一个耗时IO
await asyncio.sleep(2)
print("lucky is a nice man", i)
if __name__ == "__main__":
# loop = asyncio.get_event_loop() 此方法已弃用,使用会报错
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
tasks = []
t1 = time.time()
for url in range(1, 5):
coroutine = run(url)
task = asyncio.ensure_future(coroutine)
tasks.append(task)
loop.run_until_complete(asyncio.wait(tasks))
t2 = time.time()
print("总耗时:%.2f" % (t2 - t1))
运行结果:
lucky is a good man 1
lucky is a good man 2
lucky is a good man 3
lucky is a good man 4
lucky is a nice man 1
lucky is a nice man 3
lucky is a nice man 2
lucky is a nice man 4
总耗时:2.01
import asyncio
async def run():
print('run函数开始')
# 这个位置的asyncio.sleep() 用于携程对象中的阻塞等待
await asyncio.sleep(2)
print('run函数结束')
if __name__ == '__main__':
con = run()
asyncio.run(con)
运行结果:
run函数开始
run函数结束