0

I want to start 4 process which put an integer in queue when counter is divisible by 100.Same time another process continuously read it and print it.Please correct my code to run...I am getting an error ['Queue' object is not iterable]

from multiprocessing import Lock, Process, Queue, current_process
import time
import queue 
def doFirstjob(process_Queue):
 i=0
 while True:
 if i%100==0:
 process_Queue.put(i)
 else:
 i+=1
def doSecondjob(process_Queue):
 while(1):
 if not process_Queue.Empty:
 task = process_Queue.get()
 print("task: ",task)
 else:
 time.sleep(0.2)
def main():
 number_of_processes = 4
 process_Queue = Queue()
 processes = []
 process_Queue.put(1)
 q = Process(target=doSecondjob, args=(process_Queue))
 q.start()
 for w in range(number_of_processes):
 p = Process(target=doFirstjob, args=(process_Queue))
 processes.append(p)
 p.start()
if __name__ == '__main__':
 main()
asked Dec 4, 2018 at 12:22
1
  • put a comma after process_Queue in args, Commented Dec 4, 2018 at 12:25

1 Answer 1

1

You were getting error because Process was expecting a list/tuple in arguments/args.

Also instead of Empty it should be empty.

change the code to below.

from multiprocessing import Lock, Process, Queue, current_process
import time
import queue 
def doFirstjob(process_Queue):
 i=0
 while True:
 print("foo")
 if i%100==0:
 process_Queue.put(i)
 else:
 i+=1
def doSecondjob(process_Queue):
 while(1):
 print("bar")
 if not process_Queue.empty:
 task = process_Queue.get()
 print("task: ",task)
 else:
 time.sleep(0.2)
def main():
 number_of_processes = 4
 process_Queue = Queue()
 processes = []
 process_Queue.put(1)
 q = Process(target=doSecondjob, args=(process_Queue,))
 q.start()
 for w in range(number_of_processes):
 p = Process(target=doFirstjob, args=(process_Queue,))
 processes.append(p)
 p.start()
if __name__ == '__main__':
 main()
answered Dec 4, 2018 at 12:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.