0

I am trying to feed both an event and a queue to my worker threads. The queue is for them to talk to each other the event is for signalling them to stop working when needed. I keep getting this error and I cant quite figure out what is wrong with my definition constructors

Here is the error:

 Traceback (most recent call last):
   File "dualThreaded.py", line 52, in <module>
    c = get_Count(q, e)
 TypeError: __init__() takes exactly 2 arguments (3 given)

Here is my code:

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

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

     def run(self, e):
         i = 0
     run = True
         while run == True:
             print 'putting' 
             self.q.put('foo %d' % i ) 
             time.sleep(.5)
          if e.is_set():
             run = False

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

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

             except Empty:
                 print 'Nothing to process atm'
                 e.set()
             time.sleep(.2)



 if __name__ == '__main__':

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

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

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

     p1.join()
     p2.join()

edit fixed typos:

Kyle Sponable
  • 735
  • 1
  • 12
  • 31
  • Your code example doesn't correspond with the error message. There is no line 52, and there is no `get_Proc_Count` class. Also, the indenting is messed up. Please read the guidance on how to produce a [mcve]. – ekhumoro Oct 21 '16 at 17:53

1 Answers1

0

Ignoring your inaccurate stack frame for a moment, consider c = get_Count(q, e). get_Count's initializer def __init__(self, q): only takes one parameter after the mandatory self, but you call it with two. I've updated your code to include e in your initializer and do some minor cleanup. it kinda runs but you will likely need to work out the next issue.

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' 
            self.q.put('foo %d' % i ) 
            time.sleep(.5)
            if self.e.is_set():
                run = False

class read_Count(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

            except Empty:
                print 'Nothing to process atm'
                self.e.set()
            time.sleep(.2)


if __name__ == '__main__':

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

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

     p2 = Process(target=l.run)
     p2.start()
tdelaney
  • 73,364
  • 6
  • 83
  • 116