Kyle Sponable
Kyle Sponable

Reputation: 735

Multiprocess Event not triggering

I am trying to use an event set from my linux function to kill off both my linux and my get_Count functions the event is set and viewable from both but the get count stops and the linux goes into an infinite loop. Why wont the kill logic work? Ive tried both exit and break and you can see where they print the booleans.

 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'

Upvotes: 0

Views: 1027

Answers (1)

Dinesh Pundkar
Dinesh Pundkar

Reputation: 4196

From your code :

  • In run function of get_Count() class, you are checking event is set or not as if e.is_set() == True:. However, it should be like if self.e.is_set():
  • Instead of exit(), use break.
  • In run function of Script() class, you are setting event as e.set(). It should be as self.e.set().
  • break (exit) in Script class should be out of exception. (Reason for infinite loop)
  • Using modules as from module import something, use direct import. For your code, it will make code more readable.

Code :

In below code , you can track the changes which I did by the tag #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'

Output:

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>

Upvotes: 1

Related Questions