tags:

views:

1121

answers:

1

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

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?

+6  A: 

For your second example, you already gave the explanation yourself---Queue is a module, which cannot be called.

For the third example: I assume that you use Queue.Queue together with multiprocessing, which is not possible. Queue.Queue is made for data interchange between different threads inside the same process (using the threading module). The multiprocessing queues are for data interchange between different Python processes. While the API looks similar (it's designed to be that way), the underlying mechanisms are fundamentally different.

  • multiprocessing queues exchange data by pickling (serializing) objects and sending them through pipes.
  • Queue.Queue uses a data structure that is shared between threads and locks/mutexes for correct behaviour.
Torsten Marek