python队列&多处理队列:它们的行为如何? [英] python queue & multiprocessing queue: how they behave?

查看:72
本文介绍了python队列&多处理队列:它们的行为如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个示例代码有效(我可以在文件中写一些东西):

This sample code works (I can write something in the file):

from multiprocessing import Process, Queue

queue = Queue()
def _printer(self, queue):
    queue.put("hello world!!")

def _cmdDisp(self, queue):
    f = file("Cmd.log", "w")
    print >> f, queue.get()
    f.close()

取而代之的是另一个示例:(errormsg: 'module' object is not callable)

instead this other sample not: (errormsg: 'module' object is not callable)

import Queue

queue = Queue()
def _printer(self, queue):
    queue.put("hello world!!")

def _cmdDisp(self, queue):
    f = file("Cmd.log", "w")
    print >> f, queue.get()
    f.close()

这个其他样本不是(我不能在文件中写一些东西):

this other sample not (I cannot write something in the file):

import Queue

queue = Queue.Queue()
def _printer(self, queue):
    queue.put("hello world!!")

def _cmdDisp(self, queue):
    f = file("Cmd.log", "w")
    print >> f, queue.get()
    f.close()

有人可以解释这些差异吗?以及这样做的权利?

Can someone explain the differences? and the right to do?

推荐答案

Queue.Queue

  • 专为在 并发环境 中工作而创建,由 threading 模块生成.

    Queue.Queue

    • Was created to work in concurrent environments spawned with the threading module.

      每个线程共享一个对Queue.Queue 对象的引用.此处不会发生数据的复制或序列化,所有线程都可以访问队列中的相同数据.

      Each thread shares a reference to the Queue.Queue object among them. No copying or serialization of data happens here and all the threads have access to the same data inside the queue.

      • 专为在 并行环境 中工作而创建,由 multiprocessing 模块生成.

      • Was created to work in parallel environments spawned with the multiprocessing module.

      每个进程都可以访问其中的 multiprocessing.Queue 对象的副本.队列的内容通过pickle 序列化跨进程复制..

      Each process gets access to a copy of the multiprocessing.Queue object among them. The contents of the queue are copied across the processes via pickle serialization. .

      这篇关于python队列&多处理队列:它们的行为如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆