我正在使用 subprocess 模块来启动一个子进程(java 程序)并连接到它的输出流(stdout)。我尝试在一个类中执行以下代码并且它成功运行,但是当我尝试在一个线程中执行它时发生了一些奇怪的事情,程序无法运行。
class MiThread(threading.Thread,QMainWindow, Ui_MainWindow):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print "si se puede"
try:
from Queue import Queue, Empty
except ImportError:
#from queue import Queue, Empty # python 3.x
print "error"
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(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/',stdout=PIPE, shell=True, bufsize= 4024)
q = Queue()
t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
#t = Thread(target=enqueue_output, args=(p.stdout, q))
print "estoy en el hilo"
t.daemon = True # thread dies with the program
t.start()
# ... do other things here
while True:
try: line = q.get_nowait()
# or q.get(timeout=.1)
except Empty:
print('')
else: # got line
# ... do something with line
print "No esta null"
print line
class Workspace(QMainWindow, Ui_MainWindow): """ 此类用于管理整个 GUI `Workspace'。目前 Workspace 类似于 MainWindow """
def __init__(self):
#Llamar al hilo secundario
t= MiThread()
t.start()
我想在一个线程中执行它!请帮忙!