Go to the documentation of this file.00001
00002
00003
00004
00005
00006 import subprocess
00007 import os
00008 import sys
00009 import signal
00010 import time
00011 from threading import Thread
00012 try:
00013 from Queue import Queue, Empty
00014 except ImportError:
00015 from queue import Queue, Empty
00016
00017 ON_POSIX = 'posix' in sys.builtin_module_names
00018 BUFF_SIZE = 1
00019
00020 def get_output(out, queue):
00021 while p.poll() is None:
00022 data = out.read(BUFF_SIZE)
00023 if data:
00024 queue.put(data)
00025 else:
00026 print "nodata"
00027 else:
00028 print "error"
00029
00030
00031 cmd = ['roseus']
00032 p = subprocess.Popen(cmd,
00033 stdout=subprocess.PIPE,
00034 stderr=subprocess.PIPE,
00035 stdin=subprocess.PIPE,
00036 bufsize=BUFF_SIZE,
00037 close_fds=ON_POSIX,
00038 env=os.environ.copy(),
00039 preexec_fn=os.setpgrp)
00040
00041 stdout_queue = Queue()
00042 stderr_queue = Queue()
00043
00044 get_stdout_thread = Thread(target=get_output,
00045 args=(p.stdout, stdout_queue))
00046 get_stdout_thread.daemon = True
00047 get_stderr_thread = Thread(target=get_output,
00048 args=(p.stderr, stderr_queue))
00049 get_stderr_thread.daemon = True
00050 get_stdout_thread.start()
00051 get_stderr_thread.start()
00052
00053 while p.poll() is None:
00054 print "alive?: ", p.poll()
00055 cmd = raw_input('cmd$ ')
00056 p.stdin.write(cmd)
00057 p.stdin.flush()
00058 stdout = ""
00059 stderr = ""
00060 try:
00061 while not stdout_queue.empty():
00062 stdout += stdout_queue.get_nowait()
00063 except:
00064 pass
00065 try:
00066 while not stderr_queue.empty():
00067 stderr += stderr_queue.get_nowait()
00068 except:
00069 pass
00070 print "stdout: ", stdout
00071 print "stderr: ", stderr