Search RU
Tasks / Concurrency / Bounded queue with timeout
Statement hard

Bounded queue with timeout

Implement a thread-safe `BoundedQueue(maxsize)`.

Methods:

- `put(item, timeout=None)` — enqueue an item;
- `get(timeout=None)` — dequeue in FIFO order;
- `close()` — close the queue; calling it again is safe;
- `closed` — a `bool` property;
- `qsize()` — the current number of items.

Rules:

- `maxsize` is an integer greater than zero, otherwise `ValueError`;
- when the queue is full, `put` waits for space no longer than `timeout`;
- if no space frees up — `queue.Full`;
- when the queue is empty, `get` waits no longer than `timeout`, then raises
`queue.Empty`;
- `timeout=None` — wait without a limit;
- after `close()` a new `put` raises `QueueClosed` immediately;
- `get` after `close()` drains the remaining items and raises `QueueClosed` on
an empty queue;
- `close()` wakes threads waiting in `put` and `get`: a waiting `put` gets
`QueueClosed`;
- no item is lost or duplicated.

Declare `QueueClosed` in your own module. Take `Full` and `Empty` from `queue`.

concurrencyqueuethreadstimeout
solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

What should wake a thread waiting in `put` on a full queue? And a thread waiting
in `get` on an empty one?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts