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

4 Answers4

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,998
  • 7
  • 24
  • 51
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,756
  • 1
  • 18
  • 37
0

For Python 3.10, we can use the Queue under the queues namespace, while still using isinstance().

import multiprocessing as mp
my_q = mp.Queue()
isinstance(my_q, mp.queues.Queue)
>>> True

import asyncio
isinstance(my_q, asyncio.Queue)
>>> False

import queue
isinstance(my_q, queue.Queue)
>>> False

I know this doesn't exactly cover the original question, but since the error is much the same as with only using mp.Queue instead of mp.queues.Queue, I thought I'd add this.

luxorbis
  • 26
  • 5