0

我正在生成多个进程并在每个进程中启动检测。当我试图在进程退出之前停止检测时,检测程序似乎挂在 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()

如何解决悬挂问题?我真的需要停止仪器吗?

4

1 回答 1

0

我通过使用而不是, 和参数解决了callgrind_control悬挂问题callsystemshell=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()
于 2012-05-31T06:09:01.277 回答