Skip to content

Instantly share code, notes, and snippets.

@AbleCoder
Last active January 2, 2016 13:19
Show Gist options
  • Save AbleCoder/8309656 to your computer and use it in GitHub Desktop.
Save AbleCoder/8309656 to your computer and use it in GitHub Desktop.
IPC w/ bash | python

EXTREMELY crude example of using anonymous pipes to communicate between a bash and python script running in parallel (NOTE: Only tested in default OS X Mavericks shell)


To run it yourself: ./app1.sh | python app2.py

#!/bin/bash -
set -o nounset # Treat unset variables as an error
# doing some stuff and simulating processing time by sleeping
echo 'Initial setup'
sleep 2
# now we trigger python script
echo 'APP2_GO'
# pretending to do other stuff
echo 'super cool processing is happening right now!'
sleep 2
# now we tell python script we're done
echo 'APP2_DONE'
#!/usr/bin/python
import sys
import time
k = 0
try:
buff = ''
while True:
buff += sys.stdin.read(1)
if buff.endswith('\n'):
# output line of text received from app1.sh
print "app1 msg", (k, buff[:-1])
# handle text triggers below
if buff[:-1] == "APP2_GO":
print "Python app starts doing radical stuff now!"
elif buff[:-1] == "APP2_DONE":
print "Python app now knows the shell script is done!"
break
buff = ''
k = k + 1
except KeyboardInterrupt:
sys.stdout.flush()
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment