Skip to content

Instantly share code, notes, and snippets.

@danlkv
Last active November 4, 2020 16:52
Show Gist options
  • Save danlkv/7ddab177a56515829a138f9a10ac2434 to your computer and use it in GitHub Desktop.
Save danlkv/7ddab177a56515829a138f9a10ac2434 to your computer and use it in GitHub Desktop.
Test task: queue with timeout

Queue with timeout

A FIFO (First in first out) queue has two main methods:

  1. put(), with puts an element to the queue,
  2. get(), which returns next element.

If queue is empty, get() blocks.

Task

Create a queue in which every element that was added more than timeout seconds ago got removed from the queue.

q = TimeoutQueue(timeout=5)

q.put(1)
q.put(2)
print(q.get())
# 1
time.sleep(6)
q.put(3)
print(q.get())
# 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment