【问题标题】:Threaded Popen crashing on segmentation fault in 'bash'Threaded Popen 在“bash”中因分段错误而崩溃
【发布时间】:2016-04-03 17:20:38
【问题描述】:

我已经线程化 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 使用 threading 模块进行线程化。如上所述,当在 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

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

谢谢!!

【问题讨论】:

  • 你能创建一个小的可重现的sn-p,我可以通过复制+粘贴来执行吗?
  • @guettli:我添加了一个 sn-p。在命令列表中,可以添加一组生成分段错误的命令。
  • pds 你在最后一个 sn-p 中有一个变量 fp,它被用作 stdout,现在你正在使用 PIPE。是什么促成了这种变化?
  • @tijko,fp 是一个文件缓冲区。我正在将输出提供给文件。我在 sn-p 中更改了它。
  • 这就是我想确定的。你在哪个版本的python 上运行它?

标签: python multithreading bash popen


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2012-07-01
    相关资源
    最近更新 更多