Skip to content

Instantly share code, notes, and snippets.

@sachaarbonel
Forked from legnaleurc/asyncio.py
Created May 27, 2018 16:58
Show Gist options
  • Save sachaarbonel/efd0a292e300a41dbe1211b8f5523b23 to your computer and use it in GitHub Desktop.
Save sachaarbonel/efd0a292e300a41dbe1211b8f5523b23 to your computer and use it in GitHub Desktop.
Tornado v.s. asyncio (Python 3.5+)
#! /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())
#! /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