diff --git a/README.md b/README.md index 8560c84..cf9436b 100644 --- a/README.md +++ b/README.md @@ -2302,7 +2302,7 @@ Coroutines * **Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.** * **Coroutine definition starts with `'async'` and its call with `'await'`.** * **`'asyncio.run()'` is the main entry point for asynchronous programs.** -* **Functions wait(), gather() and as_completed() can be used when multiple coroutines need to be started at the same time.** +* **Functions wait(), gather() and as_completed() start multiple coroutines at the same time.** * **Asyncio module also provides its own [Queue](#queue), [Event](#semaphore-event-barrier), [Lock](#lock) and [Semaphore](#semaphore-event-barrier) classes.** #### Runs a terminal game where you control an asterisk that must avoid numbers: @@ -2320,11 +2320,12 @@ def main(screen): asyncio.run(main_coroutine(screen)) # Starts running asyncio code. async def main_coroutine(screen): - state = {'*': P(0, 0), **{id_: P(W//2, H//2) for id_ in range(10)}} moves = asyncio.Queue() - coros = (*(random_controller(id_, moves) for id_ in range(10)), - human_controller(screen, moves), model(moves, state), view(state, screen)) - await asyncio.wait(coros, return_when=asyncio.FIRST_COMPLETED) + state = {'*': P(0, 0), **{id_: P(W//2, H//2) for id_ in range(10)}} + ai = [random_controller(id_, moves) for id_ in range(10)] + mvc = [human_controller(screen, moves), model(moves, state), view(state, screen)] + tasks = [asyncio.create_task(cor) for cor in ai + mvc] + await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) async def random_controller(id_, moves): while True: diff --git a/index.html b/index.html index fedd1f6..f0cd107 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -1889,7 +1889,7 @@ ValueError: malformed node or string
  • Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.
  • Coroutine definition starts with 'async' and its call with 'await'.
  • 'asyncio.run(<coroutine>)' is the main entry point for asynchronous programs.
  • -
  • Functions wait(), gather() and as_completed() can be used when multiple coroutines need to be started at the same time.
  • +
  • Functions wait(), gather() and as_completed() start multiple coroutines at the same time.
  • Asyncio module also provides its own Queue, Event, Lock and Semaphore classes.
  • Runs a terminal game where you control an asterisk that must avoid numbers:

    import asyncio, collections, curses, curses.textpad, enum, random
     
    @@ -1903,11 +1903,12 @@ W, H = 15, 7
         asyncio.run(main_coroutine(screen))        # Starts running asyncio code.
     
     async def main_coroutine(screen):
    -    state = {'*': P(0, 0), **{id_: P(W//2, H//2) for id_ in range(10)}}
         moves = asyncio.Queue()
    -    coros = (*(random_controller(id_, moves) for id_ in range(10)),
    -             human_controller(screen, moves), model(moves, state), view(state, screen))
    -    await asyncio.wait(coros, return_when=asyncio.FIRST_COMPLETED)
    +    state = {'*': P(0, 0), **{id_: P(W//2, H//2) for id_ in range(10)}}
    +    ai    = [random_controller(id_, moves) for id_ in range(10)]
    +    mvc   = [human_controller(screen, moves), model(moves, state), view(state, screen)]
    +    tasks = [asyncio.create_task(cor) for cor in ai + mvc]
    +    await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
     
     async def random_controller(id_, moves):
         while True:
    @@ -2905,7 +2906,7 @@ $ pyinstaller script.py --add-data '<path>:.'  
      
     
       
    - +
    diff --git a/parse.js b/parse.js index 0a9c811..a5486e3 100755 --- a/parse.js +++ b/parse.js @@ -119,11 +119,12 @@ const COROUTINES = ' asyncio.run(main_coroutine(screen)) # Starts running asyncio code.\n' + '\n' + 'async def main_coroutine(screen):\n' + - ' state = {\'*\': P(0, 0), **{id_: P(W//2, H//2) for id_ in range(10)}}\n' + ' moves = asyncio.Queue()\n' + - ' coros = (*(random_controller(id_, moves) for id_ in range(10)),\n' + - ' human_controller(screen, moves), model(moves, state), view(state, screen))\n' + - ' await asyncio.wait(coros, return_when=asyncio.FIRST_COMPLETED)\n' + + ' state = {\'*\': P(0, 0), **{id_: P(W//2, H//2) for id_ in range(10)}}\n' + + ' ai = [random_controller(id_, moves) for id_ in range(10)]\n' + + ' mvc = [human_controller(screen, moves), model(moves, state), view(state, screen)]\n' + + ' tasks = [asyncio.create_task(cor) for cor in ai + mvc]\n' + + ' await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)\n' + '\n' + 'async def random_controller(id_, moves):\n' + ' while True:\n' +