I am trying to create 3 threads within each of 2 processes and share a queue of the type multiprocessing.JoinableQueue
among all threads. The worker_func
function simply creates the threads while the thread_func
function prints out the values it gets from the queue. The program gets stuck somewhere in the time.sleep
or in the get()
method of queue
. What am I doing wrong? I am running on a Windows computer.
JavaScript
x
52
52
1
import threading
2
from multiprocessing import Pool, Manager, JoinableQueue
3
import multiprocessing
4
from threading import Thread
5
import time
6
7
8
def thread_func(q, disp_lock):
9
with disp_lock:
10
print('thread ', threading.current_thread().name, ' in process ', multiprocessing.current_process().name ,
11
' reporting for duty')
12
while True:
13
time.sleep(0.1)
14
try:
15
val = q.get_nowait()
16
with disp_lock:
17
print('thread ', threading.current_thread().name, ' in process ', multiprocessing.current_process().name , ' got value: ',val)
18
q.task_done()
19
except:
20
with disp_lock:
21
print('queue is empty: ', q.qsize())
22
23
def worker_func(num_threads, q, disp_lock):
24
25
threads = []
26
for i in range(num_threads):
27
thread = Thread(target= thread_func, args=( q, disp_lock,))
28
thread.daemon = True
29
thread.start()
30
31
if __name__ == "__main__":
32
33
manager = Manager()
34
lock = manager.Lock()
35
36
q1 = JoinableQueue()#manager.Queue()
37
q1_length = 20
38
39
for i in range(q1_length):
40
q1.put(i)
41
42
processes = []
43
num_processes = 2 # 2 processes
44
num_threads = 3
45
for _ in range(num_processes):
46
p = multiprocessing.Process(target=worker_func, args=( num_threads, q1, lock, )) # create a new Process
47
p.daemon = True
48
p.start()
49
processes.append(p)
50
51
q1.join()
52
Advertisement
Answer
You are not allowing the threads to complete their work. Either set them as non-daemon, or explicitly wait for them to join:
JavaScript
1
13
13
1
def worker_func(num_threads, q, disp_lock):
2
threads = []
3
for i in range(num_threads):
4
thread = Thread(target=thread_func, args=(q, disp_lock,))
5
thread.daemon = True
6
thread.start()
7
8
threads.append(thread)
9
10
# Wait for them to finish
11
for thread in threads:
12
thread.join()
13