Created
July 31, 2012 20:39
-
-
Save rtomaszewski/3220289 to your computer and use it in GitHub Desktop.
An example of multithreaded python code that creates about 500 long running threads
This file contains hidden or 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 datetime | |
import time | |
from random import random | |
class ThreadClass(threading.Thread): | |
def set_id(self, id): | |
self.id=id | |
def dummy_delay(self): | |
l=[1,2] | |
for i in range(2,1000000): | |
a=(l[i-1] + l[i-2]) % 128 | |
l.append(a) | |
l.sort() | |
def run(self): | |
now = datetime.datetime.now() | |
if self.id%2 == 0: | |
self.dummy_delay() | |
print "[%s][%s] hallo from %d" % (self.getName(), now, self.id ) | |
else: | |
print "[%s][%s] hallo from %d" % (self.getName(), now, self.id ) | |
if __name__ == '__main__': | |
all=[] | |
for i in range(1000): | |
t = ThreadClass() | |
t.set_id(i) | |
all.append(t) | |
t.start() | |
main_thread = threading.currentThread() | |
for t in threading.enumerate(): | |
if t is main_thread: | |
continue | |
t.join() | |
print("\nend") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment