如何从其他进程中杀死进程?

时间:2013-04-20 16:54:24

标签: python time while-loop multiprocessing exit

我这里有2个进程:

def listen() :
   while 1 :
      data = sock.recv(1024)
      ip_header = data[:20]
      ips = ip_header[-8:-4]
      source = '%i.%i.%i.%i' % (ord(ips[0]), ord(ips[1]), ord(ips[2]), ord(ips[3]))
      print 'Ping from %s' % source

在这里:

def timerkill() :
   print "Launch proc2"
   print times
   time.sleep(int(times))
   print "End of proc2"

当第一个进程在“数据”中找到任何内容然后第一个进程执行其工作时,我尝试启动第二个进程。在“时间”结束时,我需要从第二个进程结束第一个进程。

我不知道怎么做。如果有人可以帮助我。

Ps:如果我的英语不是很好,我很抱歉。

1 个答案:

答案 0 :(得分:1)

添加runProc1等全局变量并将其设置为True。然后,在listen函数中将while 1替换为while runProc1==True。在timerkill函数中,添加

global runProc1
runProc1 = False

time.sleep(int(times))行之后