First commit
This commit is contained in:
55
text.py
Normal file
55
text.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
async def say_after(delay, what):
|
||||
print(1)
|
||||
await asyncio.sleep(delay)
|
||||
print(what)
|
||||
|
||||
async def say_after2(delay, what):
|
||||
print(2)
|
||||
await asyncio.sleep(delay)
|
||||
print(what)
|
||||
|
||||
async def say_after3(delay, what):
|
||||
print(3)
|
||||
await asyncio.sleep(delay)
|
||||
print(what)
|
||||
|
||||
|
||||
async def main():
|
||||
l = []
|
||||
|
||||
await say_after3(3, "delay1")
|
||||
|
||||
task1 = asyncio.create_task(
|
||||
say_after(1, 'hello'))
|
||||
|
||||
task2 = asyncio.create_task(
|
||||
say_after2(2, 'world'))
|
||||
|
||||
l.append(task1)
|
||||
l.append(task2)
|
||||
|
||||
task1.add_done_callback(l.remove)
|
||||
task2.add_done_callback(l.remove)
|
||||
|
||||
|
||||
await say_after3(3, "delay2")
|
||||
|
||||
print(f"started at {time.strftime('%X')}")
|
||||
|
||||
# Wait until both tasks are completed (should take
|
||||
# around 2 seconds.)
|
||||
# await task1
|
||||
# await task2
|
||||
|
||||
while len(l) != 0:
|
||||
print("wait...")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
print(f"finished at {time.strftime('%X')}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user