-
-
Save 8dspaces/1e1c0cba48cacc437fe4917f74c664a4 to your computer and use it in GitHub Desktop.
async examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| loop = asyncio.get_event_loop() | |
| 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__': | |
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from eventlet import sleep | |
| def hello(): | |
| sleep(3) | |
| print('Hello!') | |
| if __name__ == '__main__': | |
| hello() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from gevent import sleep | |
| def hello(): | |
| sleep(3) | |
| print('Hello!') | |
| if __name__ == '__main__': | |
| hello() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from time import sleep | |
| def hello(): | |
| sleep(3) | |
| print('Hello!') | |
| if __name__ == '__main__': | |
| hello() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment