无中断地将对象添加到队列

1 投票
2 回答
3101 浏览
提问于 2025-04-16 03:48

我想把两个对象放进一个队列里,但我需要确保这两个对象同时在两个队列中,也就是说,在这个过程中不能有任何中断——就像一个原子块那样。有没有人能提供解决方案?非常感谢...

queue_01.put(car)
queue_02.put(bike)

2 个回答

0

要同时向两个不同的队列添加数据,首先需要为这两个队列获取锁。最简单的方法是创建一个队列的子类,并使用递归锁。

import Queue # Note: module renamed to "queue" in Python 3
import threading

class MyQueue(Queue.Queue):
    "Make a queue that uses a recursive lock instead of a regular lock"
    def __init__(self):
        Queue.Queue.__init__(self)
        self.mutex = threading.RLock()

queue_01 = MyQueue()
queue_02 = MyQueue()

with queue_01.mutex:
    with queue_02.mutex:
        queue_01.put(1)
        queue_02.put(2)
1

你可以使用一个叫做 条件对象 的东西。你可以让线程等待,使用 cond.wait(),然后在队列准备好时发出信号,使用 cond.notify_all()。比如,Doug Hellman 有个很棒的 Python 每周模块博客。他的代码是用 multiprocessing 写的;这里我把它改成了 threading 的版本:

import threading
import Queue
import time

def stage_1(cond,q1,q2):
    """perform first stage of work, then notify stage_2 to continue"""
    with cond:
        q1.put('car')
        q2.put('bike')
        print 'stage_1 done and ready for stage 2'
        cond.notify_all()
def stage_2(cond,q):
    """wait for the condition telling us stage_1 is done"""
    name=threading.current_thread().name
    print 'Starting', name
    with cond:
        cond.wait()
        print '%s running' % name
def run():
    # http://www.doughellmann.com/PyMOTW/multiprocessing/communication.html#synchronizing-threads-with-a-condition-object
    condition=threading.Condition()
    queue_01=Queue.Queue()
    queue_02=Queue.Queue()    
    s1=threading.Thread(name='s1', target=stage_1, args=(condition,queue_01,queue_02))
    s2_clients=[
        threading.Thread(name='stage_2[1]', target=stage_2, args=(condition,queue_01)),
        threading.Thread(name='stage_2[2]', target=stage_2, args=(condition,queue_02)),
        ]
    # Notice stage2 processes are started before stage1 process, and yet they wait
    # until stage1 finishes
    for c in s2_clients:
        c.start()
        time.sleep(1)
    s1.start()
    s1.join()
    for c in s2_clients:
        c.join()

run()

运行这个脚本会得到

Starting stage_2[1]
Starting stage_2[2]
stage_1 done and ready for stage 2  <-- Notice that stage2 is prevented from running until the queues have been packed.
stage_2[2] running
stage_2[1] running

撰写回答