Skip to content

Instantly share code, notes, and snippets.

@rtomaszewski
Created July 31, 2012 20:39
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 rtomaszewski/3220289 to your computer and use it in GitHub Desktop.
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
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