diff --git a/data/async_http.py b/data/async_http.py index 14d3e1d0..6705e1e3 100755 --- a/data/async_http.py +++ b/data/async_http.py @@ -31,6 +31,8 @@ import time +import platform + #################### la version séquentielle import requests @@ -45,6 +47,9 @@ def sequential(urls): import asyncio import aiohttp +if platform.system()=='Windows': + """Solve RuntimeError: Event loop is closed in Windows""" + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # la première version, qui imprime simplement la fin de chaque url async def fetch(url): @@ -71,6 +76,16 @@ async def fetch2(url, i): # par rapport à la première variante mais ce n'est pas important return url +# runner +async def run_fetch(args, urls): + # sans option on utilise juste fetch + if (not args.details): + print(f"Running simple mode (fetch) on {len(urls)} URLs") + await asyncio.gather(*(fetch(url) for url in urls)) + + else: + print(f"Running detail mode (fetch2) on {len(urls)} URLs") + await asyncio.gather(*(fetch2(url, i) for i, url in enumerate(urls))) #################### # pour utiliser ce code directement depuis un terminal @@ -99,7 +114,7 @@ def main(): args = parser.parse_args() urls = args.urls - loop = asyncio.get_event_loop() + #loop = asyncio.get_event_loop() # mode séquentiel if args.sequential: @@ -111,18 +126,11 @@ def main(): # mode asynchrone else: - # sans option on utilise juste fetch - if (not args.details): - print(f"Running simple mode (fetch) on {len(urls)} URLs") - jobs = (fetch(url) for url in urls) - else: - print(f"Running detail mode (fetch2) on {len(urls)} URLs") - jobs = (fetch2(url, i) for i, url in enumerate(urls)) # il n'y a plus qu'à - beg = time.time() - loop.run_until_complete(asyncio.gather(*jobs)) + beg = time.time() + asyncio.run(run_fetch(args, urls)) print(f"duration = {time.time()-beg}s") if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/data/players.py b/data/players.py index 4b4eb38e..b7da50c6 100755 --- a/data/players.py +++ b/data/players.py @@ -74,9 +74,8 @@ def main(): args = parser.parse_args() players = predefined[args.groupnb] - loop = asyncio.get_event_loop() try: - loop.run_until_complete(players.run()) + asyncio.run((players.run())) sys.exit(0) except Exception as e: print(f"EMERGENCY {type(e)}, {e}") diff --git a/w8/w8-s2-av-code.html b/w8/w8-s2-av-code.html index eee27b49..2cf1b6b5 100644 --- a/w8/w8-s2-av-code.html +++ b/w8/w8-s2-av-code.html @@ -3,10 +3,20 @@ font-size: 11px; } +
@@ -23,6 +33,15 @@Prendre l’éditeur IDLE et faire nouveau fichier
-Faire Save As "w8s2 Vidéo.py" puis F5
@@ -31,13 +50,9 @@
-Faire Ctl-s puis F5
\ No newline at end of file diff --git a/w8/w8-s6-av-code.html b/w8/w8-s6-av-code.html index 25a9c3fd..fde37cfe 100644 --- a/w8/w8-s6-av-code.html +++ b/w8/w8-s6-av-code.html @@ -94,13 +94,22 @@loop = asyncio.get_event_loop() - -loop.run_until_complete( - asyncio.gather(famine("run1"), - famine("run2"))) +\ No newline at end of file diff --git a/w8/w8-s4-av-code.html b/w8/w8-s4-av-code.html index ba7867d6..2b346781 100644 --- a/w8/w8-s4-av-code.html +++ b/w8/w8-s4-av-code.html @@ -7,6 +7,15 @@+asyncio.run(main())+La vidéo a été tournée avec la 3.6, le cours a été mis à jour pour 3.10. +Il est préférable d'utiliser les fonctions asyncio de haut niveau, telles qu'asyncio.run(). +Le référencement de l'objet boucle ou l'appel de ses méthodes s'adresse principalement aux auteurs de code, +de bibliothèques et de frameworks de niveau inférieur, qui ont besoin d'un contrôle plus fin du comportement de la boucle d'événement. + +
+
@@ -34,6 +43,11 @@Prendre un notebook ou un éditeur python
réinitialisation de la boucle
asyncio.set_event_loop(asyncio.new_event_loop())
+
-Reprendre le fichier sous IDLE ou votre éditeur python et remplacer (après l'utilitaire) par
async def c1_stop():
+async def c1_stop(fut):
show_timer(">>> c1")
await asyncio.sleep(1)
show_timer("forking")
# fork
- asyncio.ensure_future(c2_stop())
+ asyncio.ensure_future(c2_stop(fut))
await asyncio.sleep(1)
show_timer("<<< c1")
# sera forkée par c1_stop() après une seconde
-async def c2_stop():
+async def c2_stop(fut):
show_timer(">>> c2")
await asyncio.sleep(2)
show_timer("<<< c2")
- # attention c'est une méthode bloquante
- asyncio.get_event_loop().stop()
+ # On set un résultat pour l'objet future afin de pouvoir quitter la boucle.
+ fut.set_result("End")
-asyncio.ensure_future(c1_stop())
+async def main():
+ loop = asyncio.get_running_loop()
+ fut = loop.create_future()
+ asyncio.ensure_future(c1_stop(fut))
+
+ print(await fut)
start_timer()
# s'arrête tout seul
try:
- asyncio.get_event_loop().run_forever()
+ asyncio.run(main())
except KeyboardInterrupt:
print("bye")
diff --git a/w8/w8-s7-av-code.html b/w8/w8-s7-av-code.html
index d170b9d2..383950dd 100644
--- a/w8/w8-s7-av-code.html
+++ b/w8/w8-s7-av-code.html
@@ -10,32 +10,32 @@