首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同时启动进程python3

同时启动进程python3
EN

Stack Overflow用户
提问于 2017-10-20 17:42:41
回答 1查看 76关注 0票数 1

我试图模拟Python3多处理中的生产者-消费者设计。主要问题是生产者启动,但消费者直到生产者完成(在这种情况下,消费者没有启动,因为生产者永远不会结束)。

以下是代码:

代码语言:javascript
复制
#!/usr/bin/python3

from scapy.all import *
from queue import Queue
from multiprocessing import Process

queue = Queue()

class Producer(Process):
    def run(self):
        global queue
        print("Starting producer thread")
        sniff(iface="wlan1mon", store=0, prn=pkt_callback)

def pkt_callback(pkt):
    queue.put(pkt)
    print(queue.qsize())

class Consumer(Process):
    def run(self):
        global queue
        while True:
            pkt = queue.get()
            queue.task_done()
            if pkt.haslayer(Dot11):
                print("**Packet with Dot11 layer has been processed")
            else:
                print("--Packet without Dot11 layer has been processed")



if __name__ == '__main__':
    Producer().start()
    Consumer().start()

我不知道我的代码有什么问题。我使用多线程来测试它,它可以工作,所以我想我对多重处理有一些误解。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-20 18:34:32

我不确定您的队列是否是共享内存对象。我认为你的生产者在给内存中的一个队列写信,而你的消费者正在从它记忆中的队列中读取,但是它们不是同一个内存,所以他们不相互交谈。我想你需要一个“经理”把它包裹起来。去看医生。https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes

或者使用队列的多处理版本。再次来自docs:https://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46854555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档