I have the below python script:
script.py
from threading import Thread
def func():
while True:
pass
a = Thread(target=func)
a.start()
print('started the thread')
When running it from the terminal (python script.py), it prints started the thread and hangs in there. Coming from the C background I know when the main thread completes the remaining threads are terminated as well. Is that not the case in Python? Will the python wait even without a call to a.join()? Is there a concept of main thread in python or all threads are same?
There is a "main thread" in Python, which is generally the thread that started the Python interpreter. Threads created by the threading module have some layers of behavior on top of native OS threads. One of those behaviors is that when the interpreter exits, part of normal shutdown processing is that the main thread joins each non-daemon threading.Thread - so, yes, Python normally waits for all other threads to end.
Whether daemon threading.Threads are forcefully shut down when Python exits is up to the OS, although I believe all major operating systems now do kill them.
If you want to exit the interpreter regardless of whether non-daemon threading.Threads are running, you need to kill the job, or call os._exit(). Note the leading underscore! It's not recommended, but is there if you need it.
Related
In general, what does, or should the main thread be doing in a thread pool / tasked based application? Tasks are added to a queue, main thread decides which task to perform, once the tasks are working, should the main task pick up a task to perform? Should it sleep at all to avoid unneccessarily spinning, or would that be to risky to miss work/tasks?
In pseudocode/ this is my understanding:
main():
# Lets say there are 4 threads in the pool
init_thread_pool(4)
while (1):
# could either run (have the logic) in main() or run_thread_pool()
# Which task should run, are there any tasks at all
run_thread_pool_tasks()
# Should anything be done here? run a task? sleep? spin?
The main thread executes whatever code the programmer wrote for it to execute. I've seen Java Swing applications where the main thread creates some GUI objects on startup, and then it just ends—lets Swing take over. If a program needs a long-running thread, it can use the main thread for that. In some frameworks, the program ends if main() returns, and I've seen programs where main() just loops calling sleep() after everything else is initialized.
Given the following code:
from threading import Thread
def dosomething():
for _ in range(100):
print("background thread running")
t = Thread(target=dosomething)
t.start()
The program runs as long as the background thread needs to completely go through the loop. My question is, why does the main thread wait for the background thread to finish and doesn't exit immediately after starting the background thread.
Because that's the way it is. From the threading docs:
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 or the daemon constructor argument.
I assume that you ran something like python my_script.py on the command line and wondered why it doesn't return you to a prompt until the worker thread is done?
Well if you change your code to:
from threading import Thread
def dosomething():
for _ in range(100):
print("background thread running")
t = Thread(target=dosomething, daemon=True)
t.start()
You will find that you are returned to the terminal. However your daemon thread will also die (again from the docs):
Note Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an Event.
This is because killing the program means killing the process and since that worker thread is handled by that process it dies - try this article for a fuller explanation on that.
Because that's the way it ought to be!
Back in the days before multi-threading, a program would end when its main routine ended which also, coincidentally, was when its last (and only) thread ended. When multi-threading became a thing, some languages (e.g., Python, Java) generalized the classic behavior to "program ends when last thread ends," while others (e.g., C/C++) generaized it to "program ends when its main thread ends."
Having worked in both worlds, It's my strong opinion that Python and Java got it right. The main thread is special to the extent that it's the first: It's the one that calls the program's designated entry point. But after things get going, there is no good reason why the main thread should be treated any differently from any other thread. That only complicates things---makes the behavior that much more difficult to explain.
I want to start a thread from a process and detach it and terminate the process. But the thread will be running continuously in the background. Can I achieve this with c++11 ?
I have detached my thread like this
std::thread(&thread_func, param1, param2).detach();
But it gets terminated once the process is terminated.
Detaching is not the same as running in the background. If you detach a thread then you simply tell the OS "I don't want to join the thread manually after it exits, please take care of that for me". But the OS will usually kill all child threads/processes when the main process exits.
So what you want is to run a deamon. However turning a process into a deamon (note that you can't daemonize a thread) is OS dependent. On linux you would call daemon function:
http://man7.org/linux/man-pages/man3/daemon.3.html
I don't know how to do that on Windows or other OS. Also you may want to read this:
Creating a daemon in Linux
I am running three processes simultaneously by executing them through an Ipython notebook. I want to know what is the best way to kill any or all of the threads whenever I want. I see that interrupting kernal in the notebook stops just the process 1.
From the Julia documentation:
interrupt([pids...])
Interrupt the current executing task on the specified workers. This is equivalent to pressing Ctrl-C on the local machine. If no arguments are given, all workers are interrupted.
Hi all~ I have a problem boring me so much.
Sometimes when I exit my program, there are some thread still running, in Linux system, it will cause crash after I quit the main loop. Is there any method that can kill all threads when I quit main loop?
It would help a lot if you specified your programming language and threading library of choice.
The usual way to control this type of situation (that is for a parent thread to wait until children complete before terminating) is to call a function supplied by the library, usually named join or wait.
pthread supplies you with pthread_join, for example.
If you're spawning processes via fork, you should use wait or waitpid in the parent to halt until the child completes - try man waitpid or take a look at this.
This way you can inform your children that you are about to exit via the usual means, wait until they wrap up and terminate, then cleanly exit the main loop.
Does this help? This is the least brutal way of synchronizing termination, if you want to actively kill the children threads there are alternatives, of course (like pthread_kill for pthreads, for example).
If you are using java try using the jconsole (Java Monitoring & Management Console) shipped with jdk6u23 in my case. You can get the thread name that is not killed. You can use join for that thread to complete.
But there can be program issue like, in my case i had a timer thread hanging [Timer-0] java.util.Timer to make an a timer.cancel() which closed that timer.