Skip to content

Instantly share code, notes, and snippets.

@robbintt
Last active November 17, 2015 17:46
Show Gist options
  • Save robbintt/aeaf66e8fea1f46e8944 to your computer and use it in GitHub Desktop.
Save robbintt/aeaf66e8fea1f46e8944 to your computer and use it in GitHub Desktop.
Python Threading Sample
"""
placeholder docstring
"""
import threading
import time
import random
def waitsome(n):
""" wait some time
"""
seconds = random.randint(0,n*1000)/1000
time.sleep(seconds)
print "waited: {} ms on {}.".format(1000*seconds,threading.currentThread().name)
if __name__ == "__main__":
"""
"""
# when we have made no threads, this returns 1.
# print threading.activeCount()
# this gives the main thread if no new threads have
# yet been initiated
# print threading.enumerate()
threadname = "randomthread"
mylist = []
for i in range(50):
mylist.append(threading.Thread(None, waitsome, threadname+str(i), [5]))
for thread in mylist:
thread.start()
print "Final (running) ActiveCount", threading.activeCount()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment