簡體   English   中英

在進程中創建線程並在線程之間共享隊列

[英]create threads within processes and share a queue between the threads

我正在嘗試在 2 個進程中創建 3 個線程,並在所有線程之間共享一個multiprocessing.JoinableQueue類型的隊列。 worker_func只是創建線程,而thread_func打印出它從隊列中獲取的值。 程序在time.sleepqueueget()方法中卡住了。 我究竟做錯了什么? 我在 Windows 計算機上運行。

import threading
from multiprocessing import Pool, Manager, JoinableQueue
import multiprocessing
from threading import Thread
import time


def thread_func(q, disp_lock):
    with disp_lock:
        print('thread ', threading.current_thread().name, ' in process ', multiprocessing.current_process().name ,
              ' reporting for duty')
    while True:
        time.sleep(0.1)
        try:
            val = q.get_nowait()
            with disp_lock:
                print('thread ', threading.current_thread().name, ' in process ', multiprocessing.current_process().name , ' got value: ',val)
            q.task_done()
        except:
            with disp_lock:
                print('queue is empty: ', q.qsize())

def worker_func(num_threads, q, disp_lock):

    threads = []
    for i in range(num_threads):
        thread = Thread(target= thread_func, args=( q, disp_lock,))
        thread.daemon = True
        thread.start()

if __name__ == "__main__":

    manager = Manager()
    lock    = manager.Lock()

    q1 = JoinableQueue()#manager.Queue()
    q1_length = 20

    for i in range(q1_length):
        q1.put(i)
        
    processes = []
    num_processes = 2   # 2 processes
    num_threads = 3
    for _ in range(num_processes):
        p = multiprocessing.Process(target=worker_func, args=( num_threads, q1, lock, )) # create a new Process
        p.daemon = True
        p.start()
        processes.append(p)

    q1.join()

您不允許線程完成它們的工作。 要么將它們設置為非守護進程,要么顯式等待它們加入:

def worker_func(num_threads, q, disp_lock):
    threads = []
    for i in range(num_threads):
        thread = Thread(target=thread_func, args=(q, disp_lock,))
        thread.daemon = True
        thread.start()
        
        threads.append(thread)

    # Wait for them to finish
    [thread.join() for thread in threads]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM