Skip to content

Instantly share code, notes, and snippets.

@8dspaces
Forked from miguelgrinberg/asyncio-async-await.py
Last active July 9, 2017 09:26
Show Gist options
  • Select an option

  • Save 8dspaces/1e1c0cba48cacc437fe4917f74c664a4 to your computer and use it in GitHub Desktop.

Select an option

Save 8dspaces/1e1c0cba48cacc437fe4917f74c664a4 to your computer and use it in GitHub Desktop.

Revisions

  1. 8dspaces revised this gist Jul 9, 2017. 1 changed file with 19 additions and 5 deletions.
    24 changes: 19 additions & 5 deletions asyncio-async-await.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,24 @@
    import asyncio
    loop = asyncio.get_event_loop()

    async def hello():
    await asyncio.sleep(3)
    print('Hello!')


    async def hello(task, sleep_seconds):
    print('{}:hello'.format(task))
    await asyncio.sleep(sleep_seconds)
    print('{}:world!'.format(task))


    # update original one example to make the result easily undertand, it takes 4 seconds
    if __name__ == '__main__':
    loop.run_until_complete(hello())
    from time import time

    start = time()
    tasks = [
    hello('task1', 4),
    hello('task2', 3),
    hello('task3', 2)]
    loop.run_until_complete(asyncio.wait(tasks))

    end = time()
    print(end-start)

  2. @miguelgrinberg miguelgrinberg created this gist Feb 20, 2017.
    10 changes: 10 additions & 0 deletions asyncio-async-await.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    import asyncio
    loop = asyncio.get_event_loop()

    async def hello():
    await asyncio.sleep(3)
    print('Hello!')

    if __name__ == '__main__':
    loop.run_until_complete(hello())

    14 changes: 14 additions & 0 deletions asyncio-callbacks.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    import asyncio
    loop = asyncio.get_event_loop()

    def hello():
    loop.call_later(3, print_hello)

    def print_hello():
    print('Hello!')
    loop.stop()

    if __name__ == '__main__':
    loop.call_soon(hello)
    loop.run_forever()

    11 changes: 11 additions & 0 deletions asyncio-coroutines.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    import asyncio
    loop = asyncio.get_event_loop()

    @asyncio.coroutine
    def hello():
    yield from asyncio.sleep(3)
    print('Hello!')

    if __name__ == '__main__':
    loop.run_until_complete(hello())

    9 changes: 9 additions & 0 deletions eventlet-greenlets.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    from eventlet import sleep

    def hello():
    sleep(3)
    print('Hello!')

    if __name__ == '__main__':
    hello()

    9 changes: 9 additions & 0 deletions gevent-greenlets.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    from gevent import sleep

    def hello():
    sleep(3)
    print('Hello!')

    if __name__ == '__main__':
    hello()

    8 changes: 8 additions & 0 deletions sync.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    from time import sleep

    def hello():
    sleep(3)
    print('Hello!')

    if __name__ == '__main__':
    hello()
    13 changes: 13 additions & 0 deletions twisted-callbacks.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    from twisted.internet import reactor

    def hello():
    reactor.callLater(3, print_hello)

    def print_hello():
    print('Hello!')
    reactor.stop()

    if __name__ == '__main__':
    reactor.callWhenRunning(hello)
    reactor.run()

    18 changes: 18 additions & 0 deletions twisted-coroutines.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    from twisted.internet import reactor
    from twisted.internet.defer import inlineCallbacks, Deferred

    def sleep(secs):
    d = Deferred()
    reactor.callLater(secs, d.callback, None)
    return d

    @inlineCallbacks
    def hello():
    yield sleep(3)
    print('Hello!')
    reactor.stop()

    if __name__ == '__main__':
    reactor.callWhenRunning(hello)
    reactor.run()