# queue.Empty() and queue.Full() not available in multiprocessing namespace

**URL:** <https://discuss.python.org/t/queue-empty-and-queue-full-not-available-in-multiprocessing-namespace/36297>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 15, 2023, 5:22pm UTC](https://discuss.python.org/t/queue-empty-and-queue-full-not-available-in-multiprocessing-namespace/36297 "2023-10-15T17:22:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slurms\_MacKenzie](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/slurms_mackenzie/32/9163_2.png) [@Slurms\_MacKenzie](https://discuss.python.org/u/Slurms_MacKenzie)\
**Post date:** [October 15, 2023, 5:22pm UTC](https://discuss.python.org/t/queue-empty-and-queue-full-not-available-in-multiprocessing-namespace/36297/1 "2023-10-15T17:22:43Z")

</div>

Hello all,

I’m a bit confused about how namespaces work when importing multiple modules. Till now my understanding has been that the interpreter maintains a namespace for each module, and when a callable object invoked from ` __main__ ` references a global variable, it is the namespace of the module in which the callable is defined that is searched for the global, NOT that of ` __main__ ` (unless of course it is actually defined in ` __main__ `).

I thought it is for this reason that callables designed to be imported by another module cannot rely on global variables defined in that module; they may rely on constants defined in their own module, but failing that any objects they require must be passed in to them.

I still think that’s all true but I can’t work out how it relates to this quote from the `multiprocessing` documentation.

> [`multiprocessing`](https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing) uses the usual [`queue.Empty`](https://docs.python.org/3/library/queue.html#queue.Empty) and [`queue.Full`](https://docs.python.org/3/library/queue.html#queue.Full) exceptions to signal a timeout. They are not available in the [`multiprocessing`](https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing) namespace so you need to import them from [`queue`](https://docs.python.org/3/library/queue.html#module-queue).

According to my current understanding, a method of `multiprocessing.Queue` that tried to raise `queue.Empty` or `queue.Full` would need them to be defined in, or imported by `multiprocessing` itself.

I hope that makes sense, and if it does I’d appreciate some guidance!

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [October 15, 2023, 6:14pm UTC](https://discuss.python.org/t/queue-empty-and-queue-full-not-available-in-multiprocessing-namespace/36297/2 "2023-10-15T18:14:31Z")

</div>

Yes, multiprocessing must somewhere import the exceptions from queue in order to raise them. But `multiprocessing.Empty` and `multiprocessing.Full` are intentionally name errors.

You should import them directly from queue, and not try to find them in one of the multiprocessing submodules, if you want to catch them in an exception clause.

---

<div class="post-metadata">

**Author:** ![Slurms\_MacKenzie](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/slurms_mackenzie/32/9163_2.png) [@Slurms\_MacKenzie](https://discuss.python.org/u/Slurms_MacKenzie)\
**Post date:** [October 15, 2023, 6:33pm UTC](https://discuss.python.org/t/queue-empty-and-queue-full-not-available-in-multiprocessing-namespace/36297/3 "2023-10-15T18:33:20Z")

</div>

> [@tjreedy](#):
>
> You should import them directly from queue, and not try to find them in one of the multiprocessing submodules, if you want to catch them in an exception clause.

Thanks. Just to clarify, is it that methods of multiprocessing.Queue can _raise_ the exceptions, and to do this they must be imported by multiprocessing. But for them to be caught in ` __main__ `, it also needs to import them?

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [October 15, 2023, 8:26pm UTC](https://discuss.python.org/t/queue-empty-and-queue-full-not-available-in-multiprocessing-namespace/36297/4 "2023-10-15T20:26:32Z")

</div>

Yes. multiprocessing.queues imports Empty and Full and defines classes Queue, JoinableQueue(Queue), and SimpleQueue. The first two raise exceptions in various places. All three have to be initialized with an OS-dependent context. Multiprocessing.context imports these classes (but not the exceptions, which is does not need) wraps them, and ‘combines’ them with the OS default context. Multiprocessing. **init** imports context and adds the bound wrappers to its globals. The structural result:

```auto
>>> import multiprocessing
>>> multiprocessing.Queue
<bound method BaseContext.Queue of <multiprocessing.context.DefaultContext object at 0x000002610976A660>>

```

Sort of baroque, but the behavioral result is that when you call the bound wrapper, it automatically initializes the class with the appropriate default context.

Any user code, including the main script, must independently import the exceptions to catch them.
