Create a gist now

Instantly share code, notes, and snippets.

@zigg /
Last active Jan 2, 2016

a client for /bin/cat in Python 3.4 asyncio
#!/usr/bin/env python3.4
import asyncio
class CatClient(asyncio.SubprocessProtocol):
"""meows at /bin/cat"""
def ping(self):
"""send data to the process"""
data = b'meow'
print('writing data %r to subprocess' % (data,))
# Schedule another ping call
def _schedule_ping(self):
"""schedules a ping call for 1 second in the future"""
def connection_made(self, transport):
"""called when the process is ready to be conversed with"""
# This is the subprocess' stdin, not ours. We write to it
self.stdin = transport.get_pipe_transport(0)
# Sechedule the first ping call
def pipe_data_received(self, fd, data):
print('received %r on fd %d' % (data, fd))
loop = asyncio.get_event_loop()
# This coroutine will return when the subprocess has started
coro = loop.subprocess_exec(CatClient, '/bin/cat')
# Now communicate with it
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment