Created
February 21, 2022 02:17
-
-
Save msg555/36a10bb5a0c0fe8c89c89d8c05d00e21 to your computer and use it in GitHub Desktop.
Example of unfair scheduling using queue.Queue in CPython
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
from threading import Thread, get_ident | |
import time | |
from queue import Queue | |
THREAD_COUNT = 10 | |
POOL_GET_TIMEOUT = 10.0 | |
POOL_SIZE = 2 | |
pool = Queue() | |
for i in range(POOL_SIZE): | |
pool.put(i) | |
def work(): | |
""" Grabs an item from the pool (e.g. a connection pool) and does some | |
blocking work with it """ | |
pool_item = pool.get(timeout=POOL_GET_TIMEOUT) | |
print("Doing work", get_ident()) | |
time.sleep(0.05) # Simulate some blocking work | |
pool.put(pool_item) | |
def thread_start(work_items): | |
""" Example thread daemon that takes and proccesses a list of work items """ | |
for work_item in work_items: | |
work_item() | |
threads = [ | |
Thread(target=thread_start, args=([work for _ in range(1000)], )) | |
for tid in range(THREAD_COUNT) | |
] | |
for thread in threads: | |
thread.start() | |
for thread in threads: | |
thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment