多进程事件不会触发

时间:2016-10-21 20:52:56

标签: python events multiprocessing

我正在尝试使用我的linux函数中的事件集来杀死我的linux和我的get_Count函数,事件已设置并可从两者中查看但get计数停止并且linux进入无限循环。为什么杀戮逻辑不起作用?我试过退出和休息,你可以看到他们在哪里打印布尔值。

 from Queue import Empty
 from multiprocessing import Process, Queue, Event 
 import time


 class get_Count(object):
     def __init__(self, q, e): 
         self.q = q
         self.e = e 

     def run(self):
         i = 0
         run = True
         while run == True:
            print 'putting' #put get process command here 
            self.q.put('foo %d' % i ) 
            time.sleep(.5)
            print 'proc test if set ' + str(e.is_set()) 
            if e.is_set() == True:
            exit()


 class Script(object):
     def __init__(self, q, e): 
         self.q = q
         self.e = e  

    def run(self):
        while True:
            try:
               value =  self.q.get(False)
               print value
               e.set()
               print e.is_set()

            except Empty:
                print 'Nothing to process atm'
                if  e.set() == True:
                    exit()    



if __name__ == '__main__':

    e = Event()
    q = Queue()
    c = get_Count(q, e)
    l = Script(q, e)

    p1 = Process(target=c.run)
    p1.start()

    p2 = Process(target=l.run)
         p2.start()

         p1.join()
         p2.join()
         print 'I can continue'

1 个答案:

答案 0 :(得分:1)

来自您的代码:

  • run类的get_Count()函数中,您正在检查事件是否设置为if e.is_set() == True:。但是,它应该像if self.e.is_set():
  • 使用exit()
  • 而不是break
  • run)类的Script(函数中,您将事件设置为e.set()。它应该是self.e.set()
  • {li> break(退出)Script类应该不在例外。 (无限循环的原因)
  • 将模块用作from module import something,使用直接导入。对于您的代码,它将使代码更具可读性。

代码:

在下面的代码中,您可以跟踪标记 #Changed

所做的更改
#Changed
import Queue 
import multiprocessing 
import time
import sys

class get_Count(object):
     def __init__(self, q, e): 
         self.q = q
         self.e = e 

     def run(self):
         i = 0
         run = True
         while run:
            print 'putting' #put get process command here 
            self.q.put('foo %d' % i ) 
            time.sleep(.5)
            #Changed  
            print 'proc test if set ' + str(self.e.is_set()) 
            #Changed
            if self.e.is_set():
                print "Exiting get_count"
                break


class Script(object):
     def __init__(self, q, e): 
         self.q = q
         self.e = e  

     def run(self):
        while True:
            try:
               time.sleep(1)
               value =  self.q.get(False)
               print "val is ",value
               #Changed
               self.e.set()
               print "Event is ",self.e.is_set()

            except Queue.Empty:
                print 'Nothing to process atm'
                #Changed
                self.e.set()
            #Changed
            if  self.e.is_set():
               break   

if __name__ == '__main__':
    #Changed
    e = multiprocessing.Event()
    q = multiprocessing.Queue()
    c = get_Count(q, e)
    l = Script(q, e)

    p1 = multiprocessing.Process(target=c.run)
    p2 = multiprocessing.Process(target=l.run)
    p1.start()
    p2.start()

    p1.join()
    p2.join()
    print 'I can continue'

输出:

C:\Users\dinesh_pundkar\Desktop>python b.py
putting
proc test if set False
putting
proc test if set False
putting
val is  foo 0
Event is  True
proc test if set True
Exiting get_count
I can continue

C:\Users\dinesh_pundkar\Desktop>