Trying to understand python multithreading - python

Please consider this code:
import threading
def printer():
for i in range(2):
with lock:
print ['foo', 'bar', 'baz']
def main():
global lock
lock = threading.Lock()
threads = [threading.Thread(target=printer) for x in xrange(2)]
for t in threads:
t.start()
t.join()
main()
I can understand this code and it is clear: We create two threads and we run them sequentially - we run second thread only when first thread is finished. Ok, now consider another variant:
import threading
def printer():
for i in range(2):
with lock:
print ['foo', 'bar', 'baz']
def main():
global lock
lock = threading.Lock()
threads = [threading.Thread(target=printer) for x in xrange(2)]
for t in threads:
t.start()
for t in threads:
t.join()
main()
What happens here? Ok, we run them in parallel, but what is the purpose of make main thread waiting for child threads in second variant? How it can influence on the output?

In the second variant, the ordering of execution is much less defined.
The lock is released each time through the loop in printer. In both variants, you have two threads and two loops within a thread.
In the first variant, since only one thread runs at a time, you know the total ordering.
In the second variant, each time the lock is released, the thread running may change.
So you might get
thread 1 loop 1
thread 1 loop 2
thread 2 loop 1
thread 2 loop 2
or perhaps
* thread 2 loop 1
* thread 1 loop 1
* thread 1 loop 2
* thread 2 loop 2
The only constraint is that loop1 within a given thread runs before loop 2 within that thread and that the two print statements come together since the lock is held for both of them.
In this particular case I'm not sure the call to t.join() in the second variant has an observable effect. It guarantees that the main thread will be the last thread to end, but I'm not sure that in this code you can observe that in any way. In more complex code, joining the threads can be important so that cleanup actions are only performed after all threads terminate. This can also be very important if you have daemon threads, because the entire program will terminate when all non-daemon threads terminate.

To better understand the multithreading in python, you need to first understand the relationship between the main thread and the children threads.
The main thread is the entry of the program, it is created by your system when you run your script. For example, in your script, the main function is run in the main thread.
While the children thread is created by your main thread when you instanate the Thread class.
The most important thing is how the main thread controls the children thread. Basically, the instance of the Thread is everything that the main thread know about and control over this child thread. At the time when a child thread is created, this child thread does not run immediately, until the main thread call start function on this thread instance. After the start the child thread, you can assume that the main thread and the child thread is running parallelly now.
But one more important thing is how the main thread knows that the task of child thread is done. Though the main thread knows nothing about how the task is done by the child thread, it does be aware of the running status of the child thread. Thread.is_alive can check the status of a thread by the main thread. In pratice, the Thread.join function is always used to tell the main thread wait until the child thread is done. This function will block the main thread.
Okay, let's examine the two script you are confused with. For the first script:
for t in threads:
t.start()
t.join()
The children threads in the loop are started and then joined one by one. Note that start does not block main thread, while join will block the main thread wait until this child thread is done. Thus they are running sequentially.
While for the second script:
for t in threads:
t.start()
for t in threads:
t.join()
All children threads are started in the first loop. As the Thread.start function will not block the main thread, all children threadings are running parallelly after the first loop. In the second loop, the main thread will wait for the task done of each child thread one by one.
Now I think you should notice the difference between these two script: in the first one, children threads running one by one, while in the second script, they are running simultaneously.
There are other useful topics for the python threading:
(1) How to handle the Keyboard Interrupt Exception, e.g., when I want to terminate the program by Ctrl-C? Only the main thread will receive the exception, you have to handle the termination of children threads.
(2) Multithreading vs Multiprocessing. Although we are saying that threading is parallel, it is not the real parallel in CPU level. So if your application is CPU intensive, try multiprocessing, and if your application is I/O intensive, multithreading maybe sufficient.
By the way, read through the documentation of python threading section and try some code may help you understand it.
Hope this would be helpful. Thanks.

Related

Pausing the Main Thread in Python

I currently have a main function being run in Python on the main thread. This main function creates an additional thread called ArtificialPlayerControllerSynthesis to run a background process. I would like to pause the main thread for a second or two to allow the background process to finish before continuing in the main thread. However, all solutions to this issue that I can find, such as from Pausing a thread using threading class, require passing an event object as an argument to the thread I want to pause. This is not possible in my case, or would at the very least require restructuring my code and moving my main function to a new thread. Is it possible to simply pause the main thread? I am new to Python and threading, thanks in advance for yall's help.
thread = threading.Thread(target=some_func)
thread.start()
do_some_stuff_now()
thread.join() # blocks until thread finishes
do_some_stuff_later()

What happens if I don't join() a python thread?

I have a query. I have seen examples where developers write something like the code as follows:
import threading
def do_something():
return true
t = threading.Thread(target=do_something)
t.start()
t.join()
I know that join() signals the interpreter to wait till the thread is completely executed. But what if I do not write t.join()? Will the thread get closed automatically and will it be reused later?
Please let me know the answer. It's my first attempt at creating a multi-threaded application in Python 3.5.0.
A Python thread is just a regular OS thread. If you don't join it, it still keeps running concurrently with the current thread. It will eventually die, when the target function completes or raises an exception. No such thing as "thread reuse" exists, once it's dead it rests in peace.
Unless the thread is a "daemon thread" (via a constructor argument daemon or assigning the daemon property) it will be implicitly joined for before the program exits, otherwise, it is killed abruptly.
One thing to remember when writing multithreading programs in Python, is that they only have limited use due to infamous Global interpreter lock. In short, using threads won't make your CPU-intensive program any faster. They can be useful only when you perform something involving waiting (e.g. you wait for certain file system event to happen in a thread).
The join part means the main program will wait for the thread to end before continuing. Without join, the main program will end and the thread will continue.
Now if you set the daemon parameter to "True", it means the thread will depends on the main program, and it will ends if the main program ends before.
Here is an example to understand better :
import threading
import time
def do_something():
time.sleep(2)
print("do_something")
return True
t = threading.Thread(target=do_something)
t.daemon = True # without the daemon parameter, the function in parallel will continue even your main program ends
t.start()
t.join() # with this, the main program will wait until the thread ends
print("end of main program")
no daemon, no join:
end of main program
do_something
daemon only:
end of main program
join only:
do_something
end of main program
daemon and join:
do_something
end of main program
# Note : in this case the daemon parameter is useless
Without join(), non-daemon threads are running and are completed with the main thread concurrently.
Without join(), daemon threads are running with the main thread concurrently and when the main thread is completed, the daemon threads are exited without completed if the daemon threads are still running.
You can see my answer in this post explaining about it in detail.

How to break signal.pause in python?

I have a main thread which starts child threads and then does signal.pause()
def run(self):
for thread in (some_thread, some_other_thread, another_thread):
thread.start()
signal.pause()
On completion of work of child threads, the main thread doesn't terminate until it receives SIG_TERM signal.
I can't use join method to wait for child threads to complete since on doing so my program is not listening to external kill signals.
What is the best way to make the main thread come out of signal.pause()?
One way could be to send kill signal from any one of the child threads, but that won't be a good approach.

How to keep the daemon threads alive after main thread exits?

I'm working on a python application, in which the main thread creates an object, say x, of a particular class.
Then it starts one thread which starts the execution in one of the methods of this object x. The method has a while True: loop, so its infinite.
Then it starts another thread which starts the execution in another method of the same object x. This method also has a while True: infinite loop.
I have made both the threads as daemon by calling t1.setDaemon(True), but it seems both stop execution once the main thread exits.
How do I keep the children alive after the parent thread is finished?
Or should I change my design to use a cron job or process fork?
The documentation says
A thread can be flagged as a "daemon thread". The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property.
If you want to keep your program running, you must have at least one non daemon thread.

Python multithread basic questions

I am trying to learn how mutex works in multithread application, but I have some doubts. In my case I have thread which read from device, thread which write to device and synchronization thread. In sync thread I have a timer in this form:
def CheckConnection(self):
. . .
threading.Timer(1, self.CheckConnection).start()
This timer runs periodically CheckConnection function.
Could you tell me:
When thread is locked (acquired) it means that thread stops execution and waits? this is sth like pause?
What will happen when I start synchronization thread, my checkConnection function executes and timer starts, after it synchronization-thread is locked... This will stop the timer and execution of CheckConnection function?
The mymutex.acquire() call will block the thread calling it until the mutex is available, then lock the mutex (so blocking any other thread which calls mymutex.acquire()), then return so the thread can continue execution. The call to mymutex.release() releases/unlocks the mutex and the oldest thread blocked on mymutex.acquire() gets to lock the mutex and return, i.e. unblocks this other thread.
Is self.CheckConnection the same function as you are defining in def CheckConnection()? So you're trying to get the CheckConnection function called every 1 (in your case) second?. There is a nice description of how to do this in the top answer here Python threading.timer - repeat function every 'n' seconds
Each thread using the mutex should be doing something like:
themutex.acquire()
try:
print('Do some stuff')
finally:
themutex.release()
In general, the code between acquire and release, i.e. 'do some stuff', should be as quick as possible because the maximum duration of any of these sections of code where mutex has been acquired is also the maximum time another thread will be blocked (unless several threads are blocked, when obviously the maximum delay can get even longer).

Categories