python multiprocessing Queue踩坑

from multiprocessing import Process, Semaphore, Lock, Queue
import time

buffer = Queue(10)
empty = Semaphore(2)
full = Semaphore(0)
lock = Lock()

class Consumer(Process):

    def run(self):
        global buffer, empty, full, lock
        while True:
            full.acquire()
            lock.acquire()
            buffer.get()
            print('Consumer pop an element')
            time.sleep(1)
            lock.release()
            empty.release()


class Producer(Process):
    def run(self):
        global buffer, empty, full, lock
        while True:
            empty.acquire()
            lock.acquire()
            buffer.put(1)
            print('Producer append an element')
            time.sleep(1)
            lock.release()
            full.release()


if __name__ == '__main__':
    p = Producer()
    c = Consumer()
    p.daemon = c.daemon = True
    p.start()
    c.start()
    p.join()
    c.join()
    

上面这一行代码,在win10,无论如何也无法运行成功,经过一下午加一晚上的,在网上各种搜集资料以及自己不断调试,终于找到原因。

首先在知乎中搜索我上面的标题应该能找到某大佬的分析,说是因为global变量在不同的进程中只能读不能写,所以上面除了变量不能在进程中共享其余没问题。

该大佬没有提出相应的解决方案,我经过各种猜想加实验得出,可以将global变量作为类属性成员传入从而可以看做是共享。文末有我附上的改正后的代码

我调试成功之后又在网上针对性的查找在不同进程之间共享变量的方法,虽然他们大多是没有重新定义Process类子类,但是他们也是采取将global变量作为参数传入Process实例中,即作为ran调用的函数的参数。其实情况很类似。

新人踩坑,自己爬出来不易,如果帮助到你麻烦点赞加评论,并且转发出去让更多的小白避坑,感谢。

from multiprocessing import Process, Semaphore, Lock, Queue

import time

buffer = Queue(10)

empty = Semaphore(2)

full = Semaphore(0)

lock = Lock()

class Consumer(Process):

    def __init__(self,buffer,full, empty,lock):

        Process.__init__(self)

        self.buffer = buffer

        self.full = full

        self.empty = empty

        self.lock = lock

    def run(self):

        global buffer, empty, full, lock

        while True:

            self.full.acquire()

            self.lock.acquire()

            self.buffer.get()

            print('Consumer pop an element')

            time.sleep(1)

            self.lock.release()

            self.empty.release()


 

class Producer(Process):

    def __init__(self,buffer,full,lock,empty):

        Process.__init__(self)

        self.buffer = buffer

        self.full = full

        self.lock = lock

        self.empty = empty

    def run(self):

        global buffer, empty, full, lock

        while True:

            self.empty.acquire()

            self.lock.acquire()

            self.buffer.put(1)

            print('Producer append an element')

            time.sleep(1)

            self.lock.release()

            self.full.release()


 

if __name__ == '__main__':

    p = Producer(buffer,full,lock,empty)

    c = Consumer(buffer,full, empty,lock)

    p.daemon = c.daemon = True

    p.start()

    c.start()

    p.join()

    c.join()

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值