Python Eclipse线程subprocess.Popen()<terminated,exit =“”value:=“”137 =“”> </terminate,>

时间:2014-10-07 22:20:06

标签: python eclipse multithreading python-2.7 subprocess

我在Eclipse中的Ubuntu上运行python 2.7

我试图从主线程以外的线程调用subprocess.Popen。

当我从Eclipse运行此代码时:

#lsbt.py

class someThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        p = subprocess.Popen(["ls", "/usr"], stdout=subprocess.PIPE)
        out = p.communicate()
        print "Done" + out[0]

def main():

    test = someThread()
    test.daemon = True
    test.start()

    while True:
        time.sleep(3600)

整个python程序似乎退出subprocess.Popen()行。

以下是eclipse所说的调用堆栈的样子:

<terminated>lsbt_1 lsbt.py [Python Run] 
    <terminated>lsbt.py 
    lsbt.py 
    <terminated, exit value: 137>lsbt.py    

所有调试似乎也停止,并且没有任何内容打印到控制台。

当我从Eclipse中的主线程运行子进程代码时,它似乎运行良好。

subprocess.Popen运行的命令似乎并不重要,那么看起来很重要的事情就是它不是从主线程运行的。

当我从终端运行python代码时,它可以工作。

这可能是Eclipse的问题吗?

@aabarnert评论说:IIRC, errno 137 on linux is ENOTTY

1 个答案:

答案 0 :(得分:0)

一种方法是设置:

daemon = False 

我不确定为什么这适用于Eclipse,但确实如此。

来自Python文档:

A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property