Skip to content

Commit

Permalink
try asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
jszheng committed Feb 23, 2016
1 parent 1361a8d commit bfcbd8b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
87 changes: 87 additions & 0 deletions async_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/python
#
# Runner with stdout/stderr catcher
#
from sys import argv
from subprocess import Popen, PIPE
import os, io
from threading import Thread
from queue import Queue,Empty

def __main__():
if (len(argv) > 1) and (argv[-1] == "-sub-"):
import time, sys
print("Application runned!")
time.sleep(2)
print("Slept 2 second")
time.sleep(1)
print("Slept 1 additional second")
time.sleep(2)
sys.stderr.write("Stderr output after 5 seconds")
print("Eol on stdin")
sys.stderr.write("Eol on stderr\n")
time.sleep(1)
print("Wow, we have end of work!")
else:
os.environ["PYTHONUNBUFFERED"]="1"
try:
p = Popen( argv + ["-sub-"],
bufsize=0, # line-buffered
stdin=PIPE, stdout=PIPE, stderr=PIPE )
except WindowsError as W:
if W.winerror==193:
p = Popen( argv + ["-sub-"],
shell=True, # Try to run via shell
bufsize=0, # line-buffered
stdin=PIPE, stdout=PIPE, stderr=PIPE )
else:
raise
inp = Queue()
sout = io.open(p.stdout.fileno(), 'rb', closefd=False)
serr = io.open(p.stderr.fileno(), 'rb', closefd=False)
def Pump(stream, category):
queue = Queue()
def rdr():
while True:
buf = stream.read1(8192)
if len(buf)>0:
queue.put( buf )
else:
queue.put( None )
return
def clct():
active = True
while active:
r = queue.get()
try:
while True:
r1 = queue.get(timeout=0.005)
if r1 is None:
active = False
break
else:
r += r1
except Empty:
pass
inp.put( (category, r) )
for tgt in [rdr, clct]:
th = Thread(target=tgt)
th.setDaemon(True)
th.start()
Pump(sout, 'stdout')
Pump(serr, 'stderr')

while p.poll() is None:
# App still working
try:
chan,line = inp.get(timeout = 1.0)
if chan=='stdout':
print("STDOUT>>", line, "<?<")
elif chan=='stderr':
print(" ERROR==", line, "=?=")
except Empty:
pass
print("Finish")

if __name__ == '__main__':
__main__()
Empty file added asyncio_chain_coroutine.py
Empty file.
Empty file added asyncio_coroutine.py
Empty file.
Empty file added asyncio_future.py
Empty file.
Empty file added asyncio_http.py
Empty file.
35 changes: 35 additions & 0 deletions pipecmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
from subprocess import PIPE, Popen
from threading import Thread

try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # python 3.x

ON_POSIX = 'posix' in sys.builtin_module_names

def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()

p = Popen(['dir'], shell=True, stdout=PIPE, bufsize=1, close_fds=ON_POSIX)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()

# ... do other things here

# read line without blocking
wait = True
while wait:
try:
line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
print('no output yet')
else: # got line
# ... do something with line
print(line)
break

0 comments on commit bfcbd8b

Please sign in to comment.