Last active
November 17, 2015 17:46
-
-
Save robbintt/aeaf66e8fea1f46e8944 to your computer and use it in GitHub Desktop.
Python Threading Sample
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
""" | |
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