Skip to content

Instantly share code, notes, and snippets.

@M0dem
Created March 31, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0dem/ef7c4c1816fa5fc93d081556233938b9 to your computer and use it in GitHub Desktop.
Save M0dem/ef7c4c1816fa5fc93d081556233938b9 to your computer and use it in GitHub Desktop.
Test out those threads...
#!/usr/bin/env
'''
threading_test.py
In this threading test, after a thread does it's single "task", it is destroyed.
It may be slightly inefficient compared to reusing threads, but it is much more controllable.
(at least for me)
'''
from threading import Thread
import random
import sys
import time
class MyThread(Thread):
def __init__(self):
Thread.__init__(self)
# just a simple "task" ... it could be anything really
self.i = None
def run(self):
# just sleeps for a random period of time between 0.0 and 2.0 seconds
# simulates an internet connection or something...
time.sleep((random.randint(0, 10) / 10) * 2)
# just display our "task"
print("{}\n".format(self.i))
MAX_THREADS = 5
DELAY = .01
# our list of "tasks"
l = [i for i in range(0, 100)]
threads = []
if __name__ == "__main__":
try:
for i in l:
# if the thread queue is full, wait until there is an opening and start a new thread
while len(threads) >= MAX_THREADS:
for j, t in enumerate(threads):
# if that thread isn't alive, remove it
if not t.is_alive():
threads.pop(j)
# so we don't have a crazy loop
time.sleep(DELAY)
# let's make this thread!
t = MyThread()
# set our thread's "task"
t.i = i
threads.append(t)
t.start()
# we will stop the operation if CTRL-C is pressed
except KeyboardInterrupt:
print("Shutting down... please wait for the queued threads to finish.\n")
# bye-bye :P
finally:
# wait to exit until all the threads are done
while len(threads) > 0:
for i, t in enumerate(threads):
if not t.is_alive():
threads.pop(i)
print("{} \"tasks\" completed by {} threads.\n".format(len(l), MAX_THREADS))
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment