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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With