"""Supervisor tree example."""

import asyncio
import logging

from garage.asyncs.futures import awaiting, on_exit, each_completed, one_of
from garage.asyncs.processes import process
from garage.asyncs.queues import Closed, Queue


@process
async def supervisor(exit):
    print('supervisor start')
    queue = Queue()
    async with awaiting(consumer(queue)) as consumer_, \
               awaiting(producer(queue)) as producer_, \
               on_exit(consumer_.stop), \
               on_exit(producer_.stop):
        async for task in each_completed([consumer_, producer_], [exit]):
            await task
    print('supervisor stop')


@process
async def producer(exit, queue):
    async def put(item):
        await one_of([queue.put(item)], [exit])
    print('producer start')
    message = list('Hello world!')
    while message:
        await put(message.pop(0))
    queue.close()
    print('producer stop')


@process
async def consumer(exit, queue):
    async def get():
        return await one_of([queue.get()], [exit])
    print('consumer start')
    try:
        while True:
            print('consume', repr(await get()))
    except Closed:
        pass
    finally:
        print('consumer stop')


def main():
    logging.basicConfig(level=logging.DEBUG)
    print('main start')
    supervisor_ = supervisor()
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(supervisor_)
    except KeyboardInterrupt:
        pass
    finally:
        supervisor_.stop()
        loop.run_until_complete(supervisor_)
        loop.close()
    print('main stop')


if __name__ == '__main__':
    main()

@@ -36,151 +36,30 @@
 ort 
-asyncio%0Aimport logging%0A%0Afrom garage.asyncs.futures import awaiting, on_exit, each_completed, one_of%0Afrom garage.asyncs.processes import process
+logging%0A%0Aimport curio%0A
 %0Afro
@@ -104,25 +104,16 @@
 Queue%0A%0A%0A
-@process%0A
 async de
@@ -125,20 +125,16 @@
 ervisor(
-exit
 ):%0A    p
@@ -186,204 +186,112 @@
     
-async with awaiting(consumer(queue)) as consumer_, %5C%0A               awaiting(producer(queue)) as producer_, %5C%0A               on_exit(consumer_.stop), %5C%0A               on_exit(producer_.stop):
+tasks = %5B%0A        await curio.spawn(consumer(queue)),%0A        await curio.spawn(producer(queue)),
 %0A    
+%5D%0A
     
@@ -312,67 +312,35 @@
  in 
-each_completed(%5Bconsumer_, producer_%5D, %5Bexit%5D):%0A   
+curio.wait(tasks):%0A
         
  awa
@@ -331,25 +331,24 @@
 s):%0A        
- 
 await task%0A 
@@ -345,16 +345,23 @@
 ait task
+.join()
 %0A    pri
@@ -380,33 +380,24 @@
 or stop')%0A%0A%0A
-@process%0A
 async def pr
@@ -403,30 +403,24 @@
 roducer(
-exit, 
 queue):%0A
     asyn
@@ -415,81 +415,8 @@
 e):%0A
-    async def put(item):%0A        await one_of(%5Bqueue.put(item)%5D, %5Bexit%5D)%0A
     
@@ -507,16 +507,22 @@
   await 
+queue.
 put(mess
@@ -584,17 +584,8 @@
 )%0A%0A%0A
-@process%0A
 asyn
@@ -603,22 +603,16 @@
 mer(
-exit, 
 queue):%0A
     
@@ -611,80 +611,8 @@
 e):%0A
-    async def get():%0A        return await one_of(%5Bqueue.get()%5D, %5Bexit%5D)%0A
     
@@ -704,16 +704,22 @@
 r(await 
+queue.
 get()))%0A
@@ -885,111 +885,30 @@
     
-supervisor_ = supervisor()%0A    loop = asyncio.get_event_loop()%0A    try:%0A        loop.run_until_complete
+try:%0A        curio.run
 (sup
@@ -910,25 +910,26 @@
 n(supervisor
-_
+()
 )%0A    except
@@ -961,116 +961,24 @@
    p
-ass%0A    finally:%0A        supervisor_.stop()%0A        loop.run_until_complete(supervisor_)%0A        loop.close(
+rint('main quit'
 )%0A  

Update supervisor tree example