Last active
July 19, 2019 19:28
-
-
Save dexterous/e35751f25fe74d8d32e0f594d9bdf8d8 to your computer and use it in GitHub Desktop.
A quick test to see how the GIL serializes seemingly concurrent operations 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
#!/usr/bin/python | |
from __future__ import print_function | |
import sys | |
import threading | |
import Queue | |
import argparse | |
STOP = object() | |
def processor(input_queue, output_queue, func): | |
while True: | |
try: | |
item = input_queue.get(False) | |
output_queue.put((threading.current_thread(), func(item))) | |
input_queue.task_done() | |
except Queue.Empty: | |
print('{0} all done!'.format(threading.current_thread()), file=sys.stderr) | |
break | |
except: | |
raise | |
def adder(count): | |
return count + 1 | |
def printer(q): | |
while True: | |
res = q.get() | |
if res is STOP: | |
print('SQ{0:02}E{1[0]}'.format(q.qsize(), str(q.empty()))) | |
break | |
print('{0[1]:02}T{0[0].name}Q{1:02}E{2[0]} '.format(res, q.qsize(), str(q.empty())), end='') | |
def worker(**kwargs): | |
t = threading.Thread(**kwargs) | |
t.start() | |
return t | |
def argparser(): | |
parser = argparse.ArgumentParser(description='Run multithreading experiment') | |
parser.add_argument('worker_count', type=int, help='Number of worker threads to start') | |
parser.add_argument('input_size', type=int, help='Number of input items to seed') | |
return parser | |
def main(): | |
arg = argparser().parse_args() | |
input_queue = Queue.Queue() | |
output_queue = Queue.Queue() | |
for i in range(arg.input_size): | |
input_queue.put(i * 2) | |
workers = map( | |
lambda i: worker(name='{0:02}'.format(i + 1), target=processor, args=(input_queue, output_queue, adder)), | |
range(arg.worker_count) | |
) | |
pt = threading.Thread(target=printer, args=(output_queue,)) | |
pt.start() | |
for w in workers: | |
print('{0} : {1}'.format(w, w.is_alive()), file=sys.stderr) | |
input_queue.join() | |
output_queue.put(STOP) | |
pt.join() | |
if __name__ == '__main__': | |
main() | |
# testing with | |
# for i in {01..50}; do echo -n "$i."$(python prog.py 4 24 2>/dev/null)" "; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment