tuoheng_algN/vodsdk/test/协程/协程笔记.py

35 lines
748 B
Python

import asyncio
import time
async def sleep(delay):
time.sleep(delay)
print("1111")
async def say_after(delay, what):
await sleep(delay)
print(what)
async def main():
task1 = asyncio.create_task(say_after(1, 'hello'))
task2 = asyncio.create_task(say_after(2, 'world'))
# await task1
# await task2
await asyncio.gather(task1, task2)
# await say_after(1, 'hello')
# await say_after(2, 'world')
start = time.time()
loop = asyncio.new_event_loop()
print(loop)
asyncio.set_event_loop(loop)
task1 = loop.create_task(say_after(1, 'hello'))
task2 = loop.create_task(say_after(2, 'world'))
loop.run_until_complete(asyncio.wait([task1, task2]))
loop.close()
# asyncio.run(main())
print(time.time() - start)