From 4ac35a722dc285327720b38834b8718202196264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 6 Feb 2025 15:45:54 +0100 Subject: [PATCH] Coroutines --- README.md | 26 +++++++++++++------------- index.html | 30 +++++++++++++++--------------- parse.js | 12 ++++++------ 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index cffe707..9aaff64 100644 --- a/README.md +++ b/README.md @@ -2327,30 +2327,30 @@ import asyncio as aio ``` ```python - = () # Creates a coroutine by calling async def function. - = await # Starts the coroutine and returns its result. - = aio.create_task() # Schedules the coroutine for execution. - = await # Returns coroutine's result. Also .cancel(). + = () # Creates a coroutine by calling async def function. + = await # Starts the coroutine and returns its result. + = aio.create_task() # Schedules the coroutine for execution. + = await # Returns coroutine's result. Also .cancel(). ``` ```python - = aio.gather(, ...) # Schedules coros. Returns list of results on await. - = aio.wait(, …) # `aio.ALL/FIRST_COMPLETED`. Returns (done, pending). - = aio.as_completed() # Iterator of coros. All return next result on await. + = aio.gather(, ...) # Schedules coros. Returns list of results on await. + = aio.wait(, return_when=…) # `'ALL/FIRST_COMPLETED'`. Returns (done, pending). + = aio.as_completed() # Iter of coros that return next result on await. ``` #### Runs a terminal game where you control an asterisk that must avoid numbers: ```python import asyncio, collections, curses, curses.textpad, enum, random -P = collections.namedtuple('P', 'x y') # Position -D = enum.Enum('D', 'n e s w') # Direction -W, H = 15, 7 # Width, Height +P = collections.namedtuple('P', 'x y') # Position +D = enum.Enum('D', 'n e s w') # Direction +W, H = 15, 7 # Width, Height def main(screen): - curses.curs_set(0) # Makes cursor invisible. - screen.nodelay(True) # Makes getch() non-blocking. - asyncio.run(main_coroutine(screen)) # Starts running asyncio code. + curses.curs_set(0) # Makes cursor invisible. + screen.nodelay(True) # Makes getch() non-blocking. + asyncio.run(main_coroutine(screen)) # Starts running asyncio code. async def main_coroutine(screen): moves = asyncio.Queue() diff --git a/index.html b/index.html index 6629569..cb29cbd 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@
- +
@@ -1902,25 +1902,25 @@ delattr(<obj>, '<name>') -
<coro> = <async_function>(<args>)         # Creates a coroutine by calling async def function.
-<obj>  = await <coroutine>                # Starts the coroutine and returns its result.
-<task> = aio.create_task(<coroutine>)     # Schedules the coroutine for execution.
-<obj>  = await <task>                     # Returns coroutine's result. Also <task>.cancel().
+
<coro> = <async_function>(<args>)          # Creates a coroutine by calling async def function.
+<obj>  = await <coroutine>                 # Starts the coroutine and returns its result.
+<task> = aio.create_task(<coroutine>)      # Schedules the coroutine for execution.
+<obj>  = await <task>                      # Returns coroutine's result. Also <task>.cancel().
 
-
<coro> = aio.gather(<coro/task>, ...)     # Schedules coros. Returns list of results on await.
-<coro> = aio.wait(<tasks>, …)             # `aio.ALL/FIRST_COMPLETED`. Returns (done, pending).
-<iter> = aio.as_completed(<coros/tasks>)  # Iterator of coros. All return next result on await.
+
<coro> = aio.gather(<coro/task>, ...)      # Schedules coros. Returns list of results on await.
+<coro> = aio.wait(<tasks>, return_when=…)  # `'ALL/FIRST_COMPLETED'`. Returns (done, pending).
+<iter> = aio.as_completed(<coros/tasks>)   # Iter of coros that return next result on await.
 

Runs a terminal game where you control an asterisk that must avoid numbers:

import asyncio, collections, curses, curses.textpad, enum, random
 
-P = collections.namedtuple('P', 'x y')    # Position
-D = enum.Enum('D', 'n e s w')             # Direction
-W, H = 15, 7                              # Width, Height
+P = collections.namedtuple('P', 'x y')     # Position
+D = enum.Enum('D', 'n e s w')              # Direction
+W, H = 15, 7                               # Width, Height
 
 def main(screen):
-    curses.curs_set(0)                    # Makes cursor invisible.
-    screen.nodelay(True)                  # Makes getch() non-blocking.
-    asyncio.run(main_coroutine(screen))   # Starts running asyncio code.
+    curses.curs_set(0)                     # Makes cursor invisible.
+    screen.nodelay(True)                   # Makes getch() non-blocking.
+    asyncio.run(main_coroutine(screen))    # Starts running asyncio code.
 
 async def main_coroutine(screen):
     moves = asyncio.Queue()
@@ -2931,7 +2931,7 @@ $ deactivate                # Deactivates the active
  
 
   
- +
diff --git a/parse.js b/parse.js index 01e4be1..0e7496d 100755 --- a/parse.js +++ b/parse.js @@ -115,14 +115,14 @@ const MATCH_EXAMPLE = const COROUTINES = 'import asyncio, collections, curses, curses.textpad, enum, random\n' + '\n' + - 'P = collections.namedtuple(\'P\', \'x y\') # Position\n' + - 'D = enum.Enum(\'D\', \'n e s w\') # Direction\n' + - 'W, H = 15, 7 # Width, Height\n' + + 'P = collections.namedtuple(\'P\', \'x y\') # Position\n' + + 'D = enum.Enum(\'D\', \'n e s w\') # Direction\n' + + 'W, H = 15, 7 # Width, Height\n' + '\n' + 'def main(screen):\n' + - ' curses.curs_set(0) # Makes cursor invisible.\n' + - ' screen.nodelay(True) # Makes getch() non-blocking.\n' + - ' asyncio.run(main_coroutine(screen)) # Starts running asyncio code.\n' + + ' curses.curs_set(0) # Makes cursor invisible.\n' + + ' screen.nodelay(True) # Makes getch() non-blocking.\n' + + ' asyncio.run(main_coroutine(screen)) # Starts running asyncio code.\n' + '\n' + 'async def main_coroutine(screen):\n' + ' moves = asyncio.Queue()\n' +