Skip to content

Instantly share code, notes, and snippets.

@kunev
Last active July 9, 2020 15:09

Revisions

  1. kunev revised this gist May 22, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions coroutines_example.py
    Original file line number Diff line number Diff line change
    @@ -51,8 +51,15 @@ def main_coro(loop):

    # get event loop
    loop = asyncio.get_event_loop()

    # schedule the main coroutine to start as soon as possible
    loop.call_soon(asyncio.async, main_coro(loop))
    # run untill explicitly stopped
    loop.run_forever()

    # instead of the above two lines we can also run
    # loop.run_until_complete(main_coro()) and remove
    # the loop parameter for main_coro and the call
    # to loop.stop() at the end of it

    loop.close()
  2. kunev created this gist May 22, 2014.
    58 changes: 58 additions & 0 deletions coroutines_example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    import asyncio

    @asyncio.coroutine
    def open_file(name):
    print("opening {}".format(name))
    return open(name)

    @asyncio.coroutine
    def close_file(file):
    print("closing {}".format(file.name))
    file.close()

    @asyncio.coroutine
    def read_data(file):
    print("reading {}".format(file.name))
    return file.read()

    @asyncio.coroutine
    def process_data(filename):
    # I want the result from open_file(filename)
    # untill it's done don't bother calling me
    file = yield from asyncio.async(open_file(filename))
    print('opened {}'.format(filename))

    # I want the result from read_data(file)
    # untill it's done don't bother calling me
    data = yield from asyncio.async(read_data(file))

    print('read {}'.format(filename))

    yield from close_file(file)


    @asyncio.coroutine
    def main_coro(loop):
    # start our tasks asynchronously in futures
    tasks = [
    asyncio.async(process_data('/etc/passwd')),
    asyncio.async(process_data('/etc/group')),
    asyncio.async(process_data('/var/log/Xorg.0.log')),
    ]

    # untill all futures are done
    while not all(task.done() for task in tasks):
    # take a short nap
    yield from asyncio.sleep(0.01)

    # we're done, so stop the event loop
    loop.stop()


    # get event loop
    loop = asyncio.get_event_loop()
    # schedule the main coroutine to start as soon as possible
    loop.call_soon(asyncio.async, main_coro(loop))
    # run untill explicitly stopped
    loop.run_forever()
    loop.close()