我正在生成多个进程并在每个进程中启动检测。 当我尝试在进程退出之前停止检测时,检测程序似乎卡在 shell 中,就好像进程已经完成并且没有进程来停止检测。 这是代码:
from os import system,fork,getpid
from glob import glob
from sys import exit
for filename in glob("py/*.py"):
f=fork()
if f==0:
system("callgrind_control --instr=on "+str(getpid()))
execfile(filename,{})
system("callgrind_control --instr=off "+str(getpid()))
exit()
如何解决挂起问题? 我真的需要停止仪器吗?
最佳答案
我通过使用 call
而不是 system
以及参数 shell=True
解决了 callgrind_control
挂起问题
from os import system,fork,getpid
from glob import glob
from subprocess import call
from multiprocessing import Process
def caller(filename):
pid=getpid()
call(["callgrind_control","--instr=on",str(pid)],shell=True)
execfile(filename,{})
call(["callgrind_control","--instr=off",str(pid)],shell=True)
for filename in glob("py/*.py"):
p=Process(target=caller,args=(filename,))
p.start()
p.join()
关于python - 停止 callgrind 中的检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10825288/