Skip to content

Instantly share code, notes, and snippets.

@takluyver
Created June 7, 2014 01:01
Show Gist options
  • Save takluyver/aef3bd23725aa34af7a3 to your computer and use it in GitHub Desktop.
Save takluyver/aef3bd23725aa34af7a3 to your computer and use it in GitHub Desktop.
Pexpect asyncio integration attempt
import asyncio
import pexpect
from pexpect.async import expect_async
p = pexpect.spawn('python')
p.setecho(False)
p.waitnoecho()
b = pexpect.spawn('bash')
b.setecho(False)
b.waitnoecho()
p.expect_exact('>>> ')
b.expect_exact("$ ")
p.sendline("import time; time.sleep(4); print('done')")
b.sendline("sleep 2 && echo 'done'")
@asyncio.coroutine
def wait_python():
yield from expect_async(p, 'done')
print("Python done")
@asyncio.coroutine
def wait_bash():
yield from expect_async(b, 'done')
print("Bash done")
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(wait_python(), wait_bash()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment