2014-10-07 24 views
0

我在Eclipse的Python的Eclipse螺紋subprocess.Popen()<終止,出口值:137>

我試圖從除主線程以外的線程中調用subprocess.Popen在Ubuntu運行蟒2.7。

當我從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()行退出。

這裏是日食說什麼調用堆棧的樣子:

<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

+0

發現了類似的問題在這裏:http://stackoverflow.com/questions/984941/python-subprocess-popen-from-a-thread 提問者@ noahd表示在其中一條評論中他的環境存在問題,但沒有說明細節。 仍在尋找 – ChrisVollo 2014-10-07 22:20:27

+1

在Linux上的IIRC,errno 137是'ENOTTY'。換句話說,出於某種原因,你的子進程認爲它是繼承了TTY,但沒有,所以當它試圖使用特定於TTY的ioctl或類似的東西時,一切都會變成地獄。所以......也許這與Eclipse的虛擬終端有關?如果從gnome-terminal下的bash運行該程序,而不是從Eclipse運行該程序,是否會發生同樣的情況? – abarnert 2014-10-07 22:35:23

+0

謝謝。 @abarnert。當我從bash運行腳本時,同樣的事情在我的Raspberry Pi上不會發生。仍在尋找解決方案,但我認爲這指出了我的一條更好的道路。 – ChrisVollo 2014-10-08 13:28:21

回答

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

+0

這應該是不相關的,因爲你在主線程上有'while True:time.sleep(3600)';只要主線程不退出,守護進程線程不會被操作系統殺死。這可能是更好的設計,不要守護線程 - 並用'test.join()'替換睡眠循環。但是,這不應該實際上_fix_什麼。 – abarnert 2014-10-08 18:36:01

+0

@abarnert,謝謝!我同意它不應該實際上_fix_什麼,但由於某種原因,它已經停止了我的代碼在eclipse中運行時崩潰。也許守護線程在eclipse中沒有分配TTY? 也許我不會將線程守護進程;這可能是一個更好的設計模式? – ChrisVollo 2014-10-16 13:14:17

+0

我不確定我會將可連接的線程稱爲「更好的設計模式」,只是一個不同的線程。例如,如果你有大量的後臺工作被分解成安全的原子部分,並且在下次啓動時很容易恢復,將它放在守護進程線程上會使應用程序的關閉變得更簡單。在這種特殊情況下,我不認爲你需要守護進程,如果在你不需要的時候使用它們會導致你沒有的問題,那麼最好停下來。我仍然想要了解這個問題,但有時候你有更緊迫的擔憂。 – abarnert 2014-10-16 17:57:55