Last active
August 29, 2015 14:02
-
-
Save ZGainsforth/e5ef4fff6a57b6172f60 to your computer and use it in GitHub Desktop.
Runs a parallelizable function in batches of threads, waiting for each batch to finish. This allows the full resources of the computer to be used, without overwheming it when you have hundreds or thousands of threads to run.
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
import threading | |
import os | |
# Normally this would be some busier function... Here it is a stub. | |
def PlotAlphaMELTS(PathName): | |
print PathName | |
# How many threads we want. | |
NumThreads = 10 | |
# Counter for the thread in the loop. | |
ThreadCount = 0 | |
# List of the threads. | |
threads = [None]*NumThreads | |
# You need a different parameter to each thread to make it do something different. | |
params = range(100) | |
# This part is really processor intensive, so let's multithread it. | |
for p in params: | |
# Set up a thread. | |
threads[ThreadCount] = threading.Thread(target=PlotAlphaMELTS, args=(p,)) | |
ThreadCount += 1 | |
# After making NumThreads threads, then we start them all. | |
if ThreadCount % NumThreads == 0: | |
for t in threads: | |
# daemon mode so they run simultaneously. | |
t.daemon = True | |
t.start() | |
# Now they are running. Wait until they all finish. | |
for t in threads: | |
t.join() | |
# And reset the counter so we do a new batch | |
ThreadCount = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment