-
-
Save sachaarbonel/efd0a292e300a41dbe1211b8f5523b23 to your computer and use it in GitHub Desktop.
Tornado v.s. asyncio (Python 3.5+)
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
#! /usr/bin/env python3 | |
import asyncio | |
import contextlib | |
async def ping(ip): | |
p = await asyncio.create_subprocess_exec('ping', '-c', '4', ip, stdout=asyncio.subprocess.PIPE) | |
async for line in p.stdout: | |
print(line) | |
await p.wait() | |
async def main(): | |
await asyncio.wait([ | |
ping('8.8.8.8'), | |
ping('8.8.4.4'), | |
]) | |
if __name__ == '__main__': | |
with contextlib.closing(asyncio.get_event_loop()) as main_loop: | |
main_loop.run_until_complete(main()) |
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
#! /usr/bin/env python3 | |
from tornado import gen, ioloop, process | |
async def ping(ip): | |
p = process.Subprocess(['ping', '-c', '4', ip], stdout=process.Subprocess.STREAM) | |
await p.stdout.read_until_close(streaming_callback=print) | |
await p.wait_for_exit() | |
async def main(): | |
await gen.multi([ | |
ping('8.8.8.8'), | |
ping('8.8.4.4'), | |
]) | |
if __name__ == '__main__': | |
main_loop = ioloop.IOLoop.instance() | |
main_loop.run_sync(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment