multithreading - Python threads are not being garbage collected -


here threading setup. on machine maximum number of threads 2047.

class worker(thread):     """thread executing tasks given tasks queue"""     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, e:                 print e             self.tasks.task_done()  class threadpool:     """pool of threads consuming tasks queue"""     def __init__(self, num_threads):         self.tasks = queue(num_threads)         _ in range(num_threads):             worker(self.tasks)      def add_task(self, func, *args, **kargs):         """add task queue"""         self.tasks.put((func, args, kargs))      def wait_completion(self):         """wait completion of tasks in queue"""         self.tasks.join() 

in other classes in module, call threadpool class above create new pool of threads. perform operations. here example:

def upload_images(self):     '''batch uploads images s3 via multi-threading'''     num_threads = min(500, len(pictures))     pool = threadpool(num_threads)      p in pictures:         pool.add_task(p.get_set_upload_img)      pool.wait_completion() 

the problem having these threads not being garbage collected.

after few runs, here error:

file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/threading.py", line 495, in start _start_new_thread(self.__bootstrap, ()) thread.error: can't start new thread

which means have hit thread limit of 2047.

any ideas? thanks.

your worker thread never returns run, thread never ends.

perhaps following run method?

def run(self):     while true:         try:             func, args, kargs = self.tasks.get()         except queue.empty:             break          try:             func(*args, **kargs)         except exception, e:             print e          self.tasks.task_done() 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -