-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathasync.py
More file actions
executable file
·33 lines (27 loc) · 893 Bytes
/
async.py
File metadata and controls
executable file
·33 lines (27 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
import asyncio
import string
from sys import argv
async def print_string(mystring, wait=0.1):
while True:
print(mystring, end="", flush=True)
await asyncio.sleep(wait)
"""
Die Event-Loop ist bei asyncio sehr wichtig.
Sie kontrolliert die Ausführung innerhalb des aktuellen (und meistens einzigen) Threads.
"""
loop = asyncio.get_event_loop()
if len(argv) > 1:
count = int(argv[1])
else:
count = 2
for letter in string.ascii_uppercase[0:count]:
# create_task erstellt einen Task aus einer Coroutine und führt ihn aus.
loop.create_task(print_string(letter))
try:
# Übergibt die Kontrolle des Ablaufs an die Loop.
loop.run_forever()
except KeyboardInterrupt: # bei Drücken von Strg+C
loop.stop() # beende die Loop
loop.close() # schließe die Loop
# siehe auch: https://docs.python.org/3.6/library/asyncio.html