Hướng dẫn get all threads python
I'm doing a project with python and in my code i had to start some threads. Now i need to call a thread to stop it from, but from another class. Is there some way to get a list of all running threads? Thanks for help. asked Jun 21, 2016 at 9:24
1 You can use threading.enumerate() : Python documentation about it here
Anders 2,23210 silver badges19 bronze badges answered Jun 21, 2016 at 10:32
kaiserkaiser 8088 silver badges11 bronze badges 2 threading.enumerate() can be used for getting the list of running threads (Thread objects). As per library reference, running threads imply
It excludes Threads that are not yet started and already terminated. You can use threading.active_count to get the length of the list returned by threading.enumerate answered Apr 9, 2019 at 7:23
VineelVineel 3965 silver badges12 bronze badges Not the answer you're looking for? Browse other questions tagged multithreading python-2.7 or ask your own question.In general, killing threads abruptly is considered a bad programming practice. Killing a thread abruptly might leave a critical resource that must be closed properly, open. But you might want to kill a thread once some specific time period has passed or some interrupt has been generated. There are the various methods by which you can kill a thread in python.
Raising exceptions in a python thread : Python3
When we run the code above in a machine and you will notice, as soon as the function raise_exception() is called, the target function run() ends. This is because as soon as an exception is raised, program control jumps out of the try block and run() function is terminated. After that join() function can be called to kill the thread. In the absence of the function run_exception(), the target function run() keeps running forever and join() function is never called to kill the thread. Set/Reset stop flag : Python3
In the above code, as soon as the global variable stop_threads is set, the target function run() ends and the
thread t1 can be killed by using t1.join(). But one may refrain from using global variable due to certain reasons. For those situations, function objects can be passed to provide a similar functionality as shown below. Python3
The function object passed in the above code always returns the value of the local variable stop_threads. This value is checked in the function run(), and as soon as stop_threads is reset, the run() function ends and the thread can be killed. Using traces to kill threads : Python3
In this code, start() is slightly modified to set the system trace function using settrace(). The local trace function is defined such that, whenever the kill flag (killed) of the respective thread is set, a SystemExit exception is raised upon the execution of the next line of code, which end the execution of the target function func. Now the thread can be killed with join(). Using the multiprocessing module to kill threads : Python3
The functionality of the above code can also be implemented by using the multiprocessing module in a similar manner, with very few changes. See the code given below. Python3
Though the interface of the two
modules is similar, the two modules have very different implementations. All the threads share global variables, whereas processes are completely separate from each other. Hence, killing processes is much safer as compared to killing threads. The Process class is provided a method, terminate(), to kill a process. Now, getting back to the initial problem. Suppose in the above code, we want to kill all the processes after 0.03s have passed. This functionality is achieved using the multiprocessing
module in the following code. Python3
Though the two modules have different implementations. This functionality provided by the multiprocessing module in the above code is similar to killing threads. Hence, the multiprocessing module can be used as a simple alternative whenever we are required to implement the killing of threads in Python. Killing Python thread by setting it as daemon : Python3
Notice that, thread t1 stays alive and prevents the main program to exit via sys.exit(). In Python, any alive non-daemon thread blocks the main program to exit. Whereas, daemon threads themselves are killed as soon as the main program exits. In other words, as soon as the main program exits, all the daemon threads are killed. To declare a thread as daemon, we set the keyword
argument, daemon as True. For Example in the given code it demonstrates the property of daemon threads. Python3
Notice that, as soon as the main program exits, the thread t1 is killed. This method proves to be extremely useful in cases where program termination can be used to trigger the killing of threads. Note that in Python, the main program terminates as soon as all the non-daemon threads are dead, irrespective of the number of daemon threads alive. Hence, the resources held by these daemon threads, such as open files, database transactions, etc. may not be released properly. The initial thread
of control in a python program is not a daemon thread. Killing a thread forcibly is not recommended unless it is known for sure, that doing so will not cause any leaks or deadlocks. Python3
Note: Above methods might not work in some situation or another, because python does not provide any direct method to kill threads. |