3

I am using python 2.7 multiprocessing on windows 7:

import multiprocessing as mp
from Queue import Queue
from multiprocessing.managers import AutoProxy

if __name__ == '__main__':
    manager = mp.Manager()
    myqueue = manager.Queue()

    print myqueue
    print type(myqueue)
    print isinstance(myqueue, Queue)
    print isinstance(myqueue, AutoProxy)

Output:

<Queue.Queue instance at 0x0000000002956B08>
<class 'multiprocessing.managers.AutoProxy[Queue]'>
False
Traceback (most recent call last):
  File "C:/Users/User/TryHere.py", line 12, in <module> print 
  isinstance(myqueue, AutoProxy) TypeError: isinstance() arg 2 must be a 
  class, type, or tuple of classes and types

My question is: I would like to check if a variable is an instance of a multiprocessing queue, how should i go about checking?

I have referred to:

Check for instance of Python multiprocessing.Connection?

Accessing an attribute of a multiprocessing Proxy of a class

but they dont seem to address my issue. Thanks in advance!

AlbertB
  • 35
  • 7

3 Answers3

4

Question: I would like to check if a variable is an instance of a multiprocessing queue, how should i go about checking?

It's a Proxy object, multiprocessing.managers.BaseProxy does match:

from multiprocessing.managers import BaseProxy
print(isinstance(myqueue, BaseProxy))
>>>True

Tested with Python: 3.4.2 and 2.7.9

stovfl
  • 14,172
  • 7
  • 20
  • 46
1

For Python 3.6, the equivalent would be

import multiprocessing
test_queue = multiprocessing.Queue()
type(test_queue) == multiprocessing.queues.Queue:
>>> True

We can choose not to perform a type-check upon a newly created Queue object as proposed by @mikeye

0

Here's what I do:

import multiprocessing as mp
my_queue = mp.Queue()
print(type(my_queue) == type(mp.Queue()))
>>>True
MikeyE
  • 1,536
  • 1
  • 15
  • 33