Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - join all threads after start_new_thread

When using the thread library, Is there a way to join all threads that were created by start_new_threads ?

for example:

try:    
    import thread 
except ImportError:
    import _thread as thread #Py3K changed it.

for url in url_ip_hash.keys(): 
    thread.start_new_thread(check_url, (url,))

How can join all threads ?

like image 610
gal Avatar asked Oct 24 '13 06:10

gal


People also ask

How do I make main thread wait for other threads in Python?

Use the Python threading module to create a multi-threaded application. Use the Thread(function, args) to create a new thread. Call the start() method of the Thread class to start the thread. Call the join() method of the Thread class to wait for the thread to complete in the main thread.

Can Python run threads in parallel?

In fact, a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations. This limitation is actually enforced by GIL. The Python Global Interpreter Lock (GIL) prevents threads within the same process to be executed at the same time.

What does the thread join () method do in Python?

Other threads can call a thread's join() method. This blocks the calling thread until the thread whose join() method is called is terminated.

How do you keep a Python thread alive?

You use the join() method for that.


1 Answers

Is there a reason you're using thread instead of the recommended Threading module? If not, you should rather use the threading.Thread objects which have a join method:

from threading import Thread


def check_url(url):
    # some code

threads = []
for url in url_ip_hash.keys():
    t = Thread(target=check_url, args=(url, ))
    t.start()
    threads.append(t)

# join all threads
for t in threads:
    t.join()
like image 67
Bastian Venthur Avatar answered Oct 13 '22 13:10

Bastian Venthur