modified
This commit is contained in:
1 parent
894afa33fe
commit
e1c3d4dace
8 files changed
+476
-38
No files matched your search
+26
-27
@@ -1,43 +1,42 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
|
||||
# Logging so konfigurieren, dass es auf der Konsole ausgegeben
|
||||
# UND in die Datei geschrieben wird (mit filemode="w")
|
||||
import asyncio
|
||||
import random
|
||||
import logging
|
||||
|
||||
# Logging konfigurieren
|
||||
logging.basicConfig(
|
||||
filename="begrüssung.log",
|
||||
filemode="w",
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||
handlers=[
|
||||
logging.FileHandler("begruessung.log", mode="w"),
|
||||
logging.StreamHandler() # Das sorgt dafür, dass es in der Konsole ausgegeben wird!
|
||||
]
|
||||
format="%(asctime)s - %(message)s"
|
||||
)
|
||||
|
||||
|
||||
async def begruesse_person(name: str):
|
||||
logging.info(f"{name} beginnt mit der Begrüßung.")
|
||||
# Coroutine für eine Begrüßung
|
||||
async def greet(name):
|
||||
|
||||
logging.info(f"Start der Begrüßung für {name}")
|
||||
|
||||
# Zufällige Wartezeit zwischen 1 und 3 Sekunden
|
||||
wartezeit = random.randint(1, 3)
|
||||
|
||||
wartezeit = random.uniform(0.5, 2.0)
|
||||
await asyncio.sleep(wartezeit)
|
||||
|
||||
logging.info(f"{name} hat die Begrüßung nach {wartezeit:.2f} Sekunden abgeschlossen.")
|
||||
return f"{name} (Erfolg)"
|
||||
# Begrüßung ausgeben
|
||||
print(f"Hallo {name}!")
|
||||
|
||||
logging.info(f"Ende der Begrüßung für {name}")
|
||||
|
||||
|
||||
# Hauptprogramm
|
||||
async def main():
|
||||
logging.info("Event Loop gestartet. Begrüßungen werden parallel ausgeführt.")
|
||||
|
||||
ergebnisse = await asyncio.gather(
|
||||
begruesse_person("Alice"),
|
||||
begruesse_person("Bob"),
|
||||
begruesse_person("Charlie")
|
||||
await asyncio.gather(
|
||||
greet("Anna"),
|
||||
greet("Max"),
|
||||
greet("Lisa")
|
||||
)
|
||||
|
||||
for ergebnis in ergebnisse:
|
||||
logging.info(f"Ergebnis verarbeitet: {ergebnis}")
|
||||
|
||||
logging.info("Event Loop beendet.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
# Programm starten
|
||||
asyncio.run(main())
|
||||
Reference in new issue
Block a user