multiprocessing.Queue() and missing sem_open

Chris Angelico rosuav at gmail.com
Thu Feb 5 17:29:05 EST 2015


On Fri, Feb 6, 2015 at 7:22 AM, Оlе Ѕtrеісhеr <ole-usenet-spam at gmx.net> wrote:
> I am just trying to prepare a package (astropy) for (Debian) Hurd. This
> os lacks a sem_open() implementation. When I now try:
>
> import multiprocessing
> q = multiprocessing.Queue()
>
> I get an ImportError with Python 2.7, but an AttributeError with Python
> 3.4. In the documentation of multiprocessing.Queue() I couldn't find any
> hint that it would throw this exception.

Neither of those errors is being consciously thrown by the Queue
class; the latter means that multiprocessing exists but it has no
Queue in it, and the former means that the entire multiprocessing
module is absent.

I'm guessing you built Python from source? If so, you should have had
a report at the bottom of the build output stating which of the
optional packages weren't built, which on 2.7 will have included
multiprocessing. You might be able to install some dependency and get
that to build, though I don't know what it would be, given that the
3.4 build managed to find it.

But this is the exact way that Python will normally signal that
something isn't available. If your platform doesn't have, say,
os.O_BINARY, because that flag has no meaning for you, then you don't
get NotImplementedError - you simply get AttributeError.

> Can I be sure that the following works also in future?
>
> try
>     q = multiprocessing.Queue()
> except (ImportError, AttributeError)
>     # handle the case of missing sem_open
>
> Or what is the correct way to catch a not working Queue caused by a
> missing sem_open() implementation?

The ImportError will come from the import statement, the
AttributeError will come from the attribute lookup - the above code
can't[1] bomb out with ImportError. So a more correct way would be
something like:

try:
    import multiprocessing
    multiprocessing.Queue
except (ImportError, AttributeError):
    # handle the absence

Or alternatively, don't even bother to try/except the import. If your
code requires multiprocessing.Queue (as opposed to just queue.Queue),
chances are you can't cope with the absence of the module.

ChrisA

[1] Yeah yeah, anything can do anything. You know what I mean.



More information about the Python-list mailing list