# Jython issue 1761 # # Timothy Reaves writes: # I run jconsole, and when I kick off my process, I see the thread count # increase, typically by 35 threads (some are ODI's). When the process has # finished, and ODI finishes it's stuff, the delta is always exactly 32; it just # keeps growing. Eventually, the VM runs out of memory, or the ulimit is hit. from threading import Thread from Queue import Queue import os, sys, time class Worker(Thread): def __init__(self, tasks): Thread.__init__(self) self.tasks = tasks self.daemon = True self.start() def run(self): while True: func, args, kargs = self.tasks.get() try: func(*args, **kargs) except Exception, exception: logging.error(exception, extra={"sbs_app":"Worker"}) self.tasks.task_done() class ThreadPool: def __init__(self, num_threads): self.tasks = Queue(num_threads) for _ in range(num_threads): Worker(self.tasks) def add_task(self, func, *args, **kargs): self.tasks.put((func, args, kargs), True) def wait_completion(self): self.tasks.join() # Client program def job(n): print "Job %d started." % n junk = '*'*1000000 time.sleep(2) print "Job %d finished." % n def run_test(N): pool = ThreadPool(32) for n in range(N): pool.add_task(job, n) pool.wait_completion() # Repeatedly run pools (are threads tidied up)? for t in range(10): print "================== Pool%3d ==================" % t run_test(100)