当前位置:   article > 正文

python multiprocessing Queue踩坑_python multiprocessing queue 丢数据

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()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/860585
推荐阅读
相关标签
  

闽ICP备14008679号