Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ctx parameter in multiprocessing.Queue

I´m trying to use a Queue from the multiprocessing.Queue module. The Implementation (https://docs.python.org/3.4/library/multiprocessing.html#exchanging-objects-between-processes) uses

q = Queue()

as an example for instantiation. If i try this, i get the following error:

TypeError: __init__() missing 1 required keyword-only argument: 'ctx'

Googling the problem brought up this:

http://bugs.python.org/issue21367

How do i know if this is fixed? Is it impossible to use multiprocessing.Queues right now? If not, how do i get the needed ctx object (and what is it?)

like image 233
user3710165 Avatar asked Jul 24 '14 18:07

user3710165


People also ask

How do you pass multiple arguments in multiprocessing in Python?

Passing Keyword Arguments to Multiprocessing Processes We can also pass in arguments corresponding to the parameter name using the kwargs parameter in the Process class. Instead of passing a tuple, we pass a dictionary to kwargs where we specify the argument name and the variable being passed in as that argument.

What is queue in multiprocessing?

Queue class. A queue is a data structure on which items can be added by a call to put() and from which items can be retrieved by a call to get(). The multiprocessing. Queue provides a first-in, first-out FIFO queue, which means that the items are retrieved from the queue in the order they were added.

What is multiprocessing Freeze_support?

multiprocessing. freeze_support() This function will allow a frozen program to create and start new processes via the multiprocessing. Process class when the program is frozen for distribution on Windows. If the function is called and the program is not frozen for distribution, then it has no effect.

Is multiprocessing queue thread safe?

Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes: Queues are thread and process safe.


2 Answers

It sounds like you're not importing Queue directly from multiprocessing. When contexts were introduced, most of the objects you import from the multiprocessing top-level package became functions which internally get a context, then pass that to an underlying class initializer, rather than being classes themselves. For example, here is what multiprocessing.Queue is now:

def Queue(self, maxsize=0):
    '''Returns a queue object'''
    from .queues import Queue
    return Queue(maxsize, ctx=self.get_context())

If you were to import multiprocessing.queues.Queue directly and try to instantiate it, you'll get the error you're seeing. But it should work fine if you import it from multiprocessing directly.

The context object tells multiprocessing which of the available methods for starting sub-processes is in use. multiprocessing.Queue uses a multiprocessing.Lock internally, which has to know the correct context to function properly.

like image 169
dano Avatar answered Oct 16 '22 23:10

dano


This is how should multiprocessing Queue class be inherited from Python 3.4 and on:

from multiprocessing.queues import Queue

class BlockedQueue(Queue):
    def __init__(self, maxsize=-1, block=True, timeout=None):
        self.block = block
        self.timeout = timeout
        super().__init__(maxsize, ctx=multiprocessing.get_context())
like image 31
Samuel Avatar answered Oct 16 '22 22:10

Samuel