2012-05-30 19 views
0

我正在产卵多个进程并在其中的每个进程中启动仪器。 当我试图在进程退出前停止检测时,检测程序似乎挂在shell中,就好像进程已经结束一样,并且没有进程来停止检测。 下面是代码:在callgrind中停止使用仪器

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() 

我怎样才能解决这个问题挂? 我真的需要停止使用仪器吗?

回答

0

我用call代替system解决callgrind_control挂的问题,与参数shell=True

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()