Skip to content

Instantly share code, notes, and snippets.

@samukasmk
Created April 10, 2024 19:45
Show Gist options
  • Save samukasmk/8004e7432a64ed09405a53112dd0b08d to your computer and use it in GitHub Desktop.
Save samukasmk/8004e7432a64ed09405a53112dd0b08d to your computer and use it in GitHub Desktop.
Example of reprocessing elements in a queue more than once

Simple queues reprocessing

Executing

python simple_queues.py

Example

image

queue = [{'value': 1, 'requeued': 0},
{'value': 2, 'requeued': 0},
{'value': 'a', 'requeued': 0},
{'value': 3, 'requeued': 0},
{'value': 4, 'requeued': 0},
{'value': 'b', 'requeued': 0},
{'value': 5, 'requeued': 0},
{'value': 6, 'requeued': 0}]
requeue_times = 3
def display_queue_values():
return [element['value'] for element in queue]
print(f'-> start\t\t\t(queue now: {display_queue_values()})')
while len(queue) > 0:
element = queue.pop(0)
if element['requeued'] > (requeue_times - 1):
print(f'-> found element requeued more than {requeue_times} times, stoping...')
break
print(f'processing element:\t{element["value"]}\t(queue now: {display_queue_values()})')
if isinstance(element['value'], str):
element['requeued'] += 1
queue.append(element)
print(f'-> requeued element:\t{element["value"]}\t(queue now: {display_queue_values()})')
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment