Skip to content

Instantly share code, notes, and snippets.

@msg555
Created February 21, 2022 02:17
Show Gist options
  • Save msg555/36a10bb5a0c0fe8c89c89d8c05d00e21 to your computer and use it in GitHub Desktop.
Save msg555/36a10bb5a0c0fe8c89c89d8c05d00e21 to your computer and use it in GitHub Desktop.
Example of unfair scheduling using queue.Queue in CPython
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