Created
March 30, 2022 21:25
-
-
Save shearichard/208e6b9cd5c2419afc1d763b81248b33 to your computer and use it in GitHub Desktop.
Example of queueing / de-queing in Python
This file contains hidden or 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
import threading, queue | |
import random | |
import time | |
''' | |
Representaive output | |
$ python queue_example.py | |
> Submission of 0 pausing for 3 | |
< Processing of 0 pausing for 2 | |
< Working on 0 | |
< Finished 0 | |
> Submission of 1 pausing for 3 | |
< Processing of 1 pausing for 1 | |
< Working on 1 | |
< Finished 1 | |
> Submission of 2 pausing for 1 | |
> Submission of 3 pausing for 0 | |
< Processing of 2 pausing for 0 | |
< Working on 2 | |
< Finished 2 | |
> Submission of 4 pausing for 2 | |
< Processing of 3 pausing for 3 | |
< Working on 3 | |
< Finished 3 | |
> Submission of 5 pausing for 1 | |
< Processing of 4 pausing for 1 | |
< Working on 4 | |
< Finished 4 | |
< Processing of 5 pausing for 0 | |
< Working on 5 | |
< Finished 5 | |
> Submission of 6 pausing for 1 | |
< Processing of 6 pausing for 1 | |
< Working on 6 | |
< Finished 6 | |
> Submission of 7 pausing for 3 | |
> Submission of 8 pausing for 0 | |
< Processing of 7 pausing for 3 | |
< Working on 7 | |
< Finished 7 | |
< Processing of 8 pausing for 3 | |
< Working on 8 | |
< Finished 8 | |
All work completed | |
''' | |
q = queue.Queue() | |
def worker(): | |
while True: | |
processing_delay = random.randint(0, 3) | |
time.sleep(processing_delay) | |
item = q.get() | |
print(f'< Processing of {item} pausing for {processing_delay}') | |
print(f'< Working on {item}') | |
print(f'< Finished {item}') | |
q.task_done() | |
# Turn-on the worker thread. | |
threading.Thread(target=worker, daemon=True).start() | |
# Send thirty task requests to the worker. | |
for item in range(9): | |
submission_delay = random.randint(0, 3) | |
time.sleep(submission_delay) | |
print(f'> Submission of {item} pausing for {submission_delay}') | |
q.put(item) | |
# Block until all tasks are done. | |
q.join() | |
print('All work completed') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment