螺纹Popen在“bash'”中的分段错误崩溃

时间:2016-04-03 17:20:38

标签: python multithreading bash popen

我已经使用Popen bash来运行命令行工具。 bash生成段错误或中止执行命令。

def FunctionToThread(args):

    su2 = Popen('bash', shell = True, stdin = PIPE, stdout=fp, env = os.environ)
    for i in commands:
        su2.stdin.write(i)
    su2.stdin.close()
    su2.wait()
    fp.close()

函数FunctionToThread使用线程模块进行线程化。如上所述,当在bash中遇到段错误时,线程终止。

我想要在try / except控件类型中捕获此段错误,最重要的是阻止我的线程终止。

我如何实现这一目标?

"""SNIPPET"""
from multiprocessing import cpu_count
import threading
from subprocess import Popen, PIPE
import os

start =0
numcores =  cpu_count()
global RESULTS, LOCK
LOCK = threading.Lock()
RESULTS = []

def ParallelRun(commands, RESULTS, LOCK):

    for i in range(0, 100):
        LOCK.acquire()
        RESULTS.append('ParallelRUn')
        LOCK.release()

    su2 = Popen('bash', shell = True, stdin = PIPE, stdout=PIPE, env = os.environ)
    for i in commands:
        su2.stdin.write(i)
    su2.stdin.close()
    err =su2.wait()


for i in range(0, numcores):
    commands = ['Enter commands which cause Segmentation Faults']
    t = threading.Thread(target=ParallelRun, args=(commands, RESULTS, LOCK))
    threads.append(t)

for t in threads:
    t.start()

for t in threads:
    t.join()


print len(RESULTS), RESULTS

要复制我的问题,请在命令列表中输入生成段错误的命令。

谢谢!

1 个答案:

答案 0 :(得分:1)

此问题已解决。事实证明,我在程序的if / else块中有一个 return 语句,它终止了我的帖子。