Last active
April 22, 2017 07:09
-
-
Save infinite-Joy/d3465bfdceb3507b7ba749f24a784e28 to your computer and use it in GitHub Desktop.
boilerplate code to implement threading
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
''' | |
taken from the raymond hettingers take on threading https://dl.dropboxusercontent.com/u/3967849/pyru/_build/html/threading.html | |
''' | |
import threading, time, random, queue | |
########################################################################################## | |
# Fuzzing is a technique for amplifying race condition errors to make them more visible | |
FUZZ = True | |
def fuzz(): | |
if FUZZ: | |
time.sleep(random.random()) | |
########################################################################################### | |
urls = [ | |
'1', | |
'2', | |
'3', | |
'4', | |
'5' | |
] | |
download_queue = queue.Queue() | |
def download_manager(): | |
'''I have exclusive rights to update the download variable''' | |
# global counter | |
while True: | |
url = download_queue.get() | |
fuzz() | |
# response = requests.get(url, timeout=3) | |
fuzz() | |
print_queue.put([ | |
'downloaded content from url %s' % url, | |
'------------------' | |
]) | |
fuzz() | |
download_queue.task_done() | |
t = threading.Thread(target=download_manager) | |
t.daemon = True | |
t.start() | |
del t | |
############################################################################### | |
counter = 0 | |
print_queue = queue.Queue() | |
def print_manager(): | |
'''I have exclusive rights to call the "print" keyword''' | |
global counter | |
while True: | |
job = print_queue.get() | |
fuzz() | |
counter += 1 | |
for line in job: | |
print(line, end='') | |
fuzz() | |
print() | |
fuzz() | |
# now write to the file | |
filename = 'somefile.txt' | |
content = '\n'.join(job) | |
with open(filename, 'a+') as fw: | |
fw.write(content) | |
fw.write('\n') | |
fuzz() | |
print_queue.task_done() | |
fuzz() | |
t = threading.Thread(target=print_manager) | |
t.daemon = True | |
t.start() | |
del t | |
#################################################################### | |
def worker(url): | |
'My job is to increment the counter and print the current count' | |
download_queue.put(url) | |
fuzz() | |
print_queue.put(['Starting up']) | |
fuzz() | |
worker_threads = [] | |
for i, url in enumerate(urls): | |
t = threading.Thread(target=worker, args=(url,)) | |
worker_threads.append(t) | |
t.start() | |
fuzz() | |
download_queue.join() | |
fuzz() | |
print_queue.put(['Finishing up']) | |
fuzz() | |
print_queue.join() | |
fuzz() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment