Python check isinstance multiprocessing.Manager().Queue()


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)


<Queue.Queue instance at 0x0000000002956B08>
<class 'multiprocessing.managers.AutoProxy[Queue]'>
Traceback (most recent call last):
  File "C:/Users/User/", 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!

Asked By: AlbertB



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

Tested with Python: 3.4.2 and 2.7.9

Answered By: stovfl

Here’s what I do:

import multiprocessing as mp
my_queue = mp.Queue()
print(type(my_queue) == type(mp.Queue()))
Answered By: MikeyE

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

Answered By: Sian Lerk Lau

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.

Answered By: luxorbis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.