线程 Popen 在 'bash' 中因分段错误而崩溃
Threaded Popen crashing on segmentation fault in 'bash'
我已经将 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
要重现我的问题,请在 commands 列表中输入生成段错误的命令。
谢谢!!
此问题已解决。事实证明,我在程序的 if/else 块中有一个 return 语句终止了我的线程。
我已经将 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
要重现我的问题,请在 commands 列表中输入生成段错误的命令。
谢谢!!
此问题已解决。事实证明,我在程序的 if/else 块中有一个 return 语句终止了我的线程。