top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to check for threads being finished in python? [CLOSED]

0 votes
2,261 views

I have a pool of worker threads, created like this:

threads = [MyThread(*args) for i in range(numthreads)]
for t in threads:
 t.start()

I then block until the threads are all done:

while any(t.isAlive() for t in threads):
 pass

Is that the right way to wait for the threads to be done? Should I stick a call to time.sleep() inside the while loop? If so, how long should I sleep? That's probably an unanswerable question, but some guidelines on
choosing the sleep time will be appreciated.

closed with the note: None
posted Jul 5, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

Using the threading module, I assume. Is there any reason you can't simply join() each thread in succession?

answer Jul 5, 2013 by anonymous
+1 vote

I think your while loop busy-waits until the threads are completed. Do this instead:

for t in threads: t.join() # optionally pass a timeout to join
answer Jul 5, 2013 by anonymous
+1 vote

If you are just going to wait for them to finish, and never use them again...

for t in threads:
 t.join()

Doesn't matter which thread ends first, this operation will either block if the thread hasn't finished or cycle to the next thread if it has.

answer Jul 5, 2013 by anonymous
Similar Questions
+1 vote

I want my thread to be killed when I receive a particular message from Master.(I want my thread to stop whatever it is doing and come out.)

I tried few things but not working,
1) thread_name.exit()
2) thread_name.daemon = True

Any other way?

+1 vote

I am working on integration of multiple GUI (Tkinter) elements, such a progress bar, label update, etc, and throughout my research i discovered that i need to have Threading modules used to distribute the calls for GUI update and processing of my main App.

My Application is broken into multiple Classes(modules), and i wanted to hear your thought on the best practices to implement the Threading model.

I was thinking to create a new py-module, and reference all other modules/class to it, and create thread.start() objects, that would execute the GUI part, other will handle GUI Updates, and other - will be doing the processing.

Please share some of your thought on this approach, or maybe you may suggest a more effective way.

0 votes

This is my first script where I want to use the python threading module. I have a large dataset which is a list of dict this can be as much as 200 dictionaries in the list. The final goal is a histogram for each dict 16 histograms on a page ( 4x4 ) - this already works.
What I currently do is a create a nested list [ [ {} ], [ {} ] ] each inner list contains 16 dictionaries, thus each inner list is a single page of 16 histograms. Iterating over the outer-list and creating the graphs takes to long. So I would like multiple inner-list to be processes simultaneously and creating the graphs in "parallel".
I am trying to use the python threading for this. I create 4 threads loop over the outer-list and send a inner-list to the thread. This seems to work if my nested lists only contains 2 elements - thus less elements than threads. Currently the scripts runs and then seems to get hung up. I monitor the resource on my mac and python starts off good using 80% and when the 4-thread is created the CPU usages drops to 0%.

My thread creating is based on the following : http://www.tutorialspoint.com/python/python_multithreading.htm

...