Python - 锁定共享资源(多处理)

时间:2014-08-27 05:13:59

标签: python multiprocessing

我想发送一个触发器来启动第二个进程。它只是在经过一定时间之后,才开始第二个过程。与锁定共享资源类似。我该怎么做?

这是我的示例代码: -

def worker():
    for i in range(1,10):
        if i == 5:
            # send a trigger to start the next event. 
            # Its like 'locking' a shared resource.

def main():
    for i in range(1,100):
        d = multiprocessing.Process(target = worker, args = ())
        d.daemon = True
        d.start()

已修改(预期输出)

Process1 loop1
Process1 loop2
Process1 loop3
Process1 loop4
1 2014-08-27 11:45:51.687848 # after this random numbers should get print.
Process2 loop1
Process2 loop2
Process2 loop3
Process2 loop4
Process1 loop5
Process1 loop6
Process1 loop7
Process1 loop8
Process1 loop9
2 2014-08-27 11:45:51.690052
Process2 loop5
Process2 loop6
Process2 loop7
Process2 loop8
Process2 loop9

当前输出

Process1 loop1
Process1 loop2
Process1 loop3
Process1 loop4
1 2014-08-27 11:45:51.687848
Process1 loop5
Process1 loop6
Process1 loop7
Process1 loop8
Process1 loop9
Process2 loop1
Process2 loop2
Process2 loop3
Process2 loop4
2 2014-08-27 11:45:51.690052
Process2 loop5
Process2 loop6
Process2 loop7
Process2 loop8
Process2 loop9

1 个答案:

答案 0 :(得分:1)

我通常更喜欢按Queue来控制流程。它为您提供了更大的灵活性,因为该进程可以根据队列中的命令执行不同的操作。

import datetime
import multiprocessing
from multiprocessing import Queue

def worker(work_queue):
    if work_queue.get() == "Start":
            for i in range(1,10):
                if i == 5:
                    # do something
                    print datetime.datetime.now() 

def main():
    worker_queues = {}
    for i in xrange(1, 6):
        q = Queue()
        worker_queues[i] = q # one queue per process here
        d = multiprocessing.Process(target = worker, args = (q,))
        d.daemon = True
        d.start()
    for wq in worker_queues.values():
        wq.put("Start")

if __name__ == "__main__":
    main()

编辑:针对您编辑的问题,您可以通过在上述解决方案中添加done queue来强制执行订单

import datetime
import multiprocessing
from multiprocessing import Queue

def worker(myid, work_queue, done_queue):
    if work_queue.get() == "Start":
        for i in range(1,10):
                if i == 5:
                    # do something
                    print myid, datetime.datetime.now() 
        done_queue.put(myid)

def main():
    worker_queues = {}
    for i in xrange(1, 6): 
        q = Queue()
        done_q = Queue()
        worker_queues[i] = (q, done_q)
        d = multiprocessing.Process(target = worker, args = (i, q, done_q))
        d.daemon = True
        d.start()


    for i in xrange(1, 6): 
        worker_queues[i][0].put("Start")
        if worker_queues[i][1].get():
            # move to next iteration
            pass

if __name__ == "__main__":
    main() 

您可以将此解决方案理解为进程之间的一种消息传递机制。