Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Threads object append to list

I am learning multi-thread in python.I often see when the program use multi thread,it will append the thread object to one list, just as following:

# imports
import threading
import time

def worker():
    print "worker...."
    time.sleep(30)

threads = []
for i in range(5):
    thread = threading.Thread(target=worker)
    threads.append(thread)
    thread.start()

I think append the thread object to list is good practice, but I don't know why should we do this?

like image 595
tudouya Avatar asked Nov 02 '15 04:11

tudouya


People also ask

Can multiple threads append to a list in Python?

We can safely append to a file from multiple threads using a mutual exclusion lock. Python provides a mutual exclusion lock, also called a mutex, via the threading. Lock class. First, we can create an instance of the lock to be shared by all threads.

Are Python objects thread-safe?

Python is not by its self thread safe. But there are moves to change this: NoGil, etc. Removing the GIL does not make functions thread-safe.

Is Python list pop thread-safe?

pop() operations are atomic, meaning that they won't be interrupted by a different thread. So if you restrict yourself to using only . append() and . pop() , then you will be thread safe.

How do you join a thread in Python?

A thread can be joined in Python by calling the Thread. join() method. This has the effect of blocking the current thread until the target thread that has been joined has terminated.


1 Answers

This is common practice. Taking your example:

# imports
import threading
import time

def worker():
    print "worker...."
    time.sleep(30)

threads = []
for i in range(5):
    thread = threading.Thread(target=worker)
    threads.append(thread)
    thread.start()

One might want to wait for every thread to finish its work:

for thread in threads:  # iterates over the threads
    thread.join()       # waits until the thread has finished work

Without storing the threads in some data structure you would have to do it (create, start, join, ...) manually:

thread_1 = threading.Thread(target=worker)
(...)
thread_n = threading.Thread(target=worker)

thread_1.start()
(...)
thread_n.start()

thread_1.join()
(...)
thread_n.join()

As you see (and can imagine): the more you work with the threads, the more "paperwork" would be created if you handle every thread manually. This fastly gets too much of a hassle. Additionally your code would be more confusing and less maintainable.

like image 70
daniel451 Avatar answered Sep 30 '22 14:09

daniel451