python simple_queues.py
Created
April 10, 2024 19:45
-
-
Save samukasmk/8004e7432a64ed09405a53112dd0b08d to your computer and use it in GitHub Desktop.
Example of reprocessing elements in a queue more than once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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