|
12345678910111213141516171819202122232425262728293031323334 |
- 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)
-
|