The beauty of Python [from cainiao to masters] -- Interpretation of threading daemon thread principle, python -- threading

Source: Internet
Author: User

The beauty of Python [from cainiao to masters] -- Interpretation of threading daemon thread principle, python -- threading

The reason is that I am reading the following piece of code. It is obviously while True. Why is there no endless code ??

class D(threading.Thread):    def __init__(self, queue):        threading.Thread.__init__(self)        self.queue = queue    def run(self):        while  True:            url = self.queue.get()            self.download_file(url)            self.queue.task_done()    def download_file(self, url):        h = urllib2.urlopen(url)        f = os.path.basename(url)+'.html'        with open(f,'wb') as f:            while  True:                c = h.read(1024)                if not c:                    break                f.write(c)if __name__ == "__main__":    urls= ['http://www.baidu.com','http://www.sina.com']    queue = Queue.Queue()    for i in range(5):        t = D(queue)        t.setDaemon(True)        t.start()    for u in urls:        queue.put(u)    queue.join()

Previously, I thought that setDaemon is just set as a background thread, without further exploring its meaning.

The key to the problem is setDaemon. In the underlying thread module, as long as the main thread ends, all other threads will end, which is obvious, when the main thread ends, python will destroy the runtime environment, and the main thread will surely end. So the thread setDaemon of the threading module is

To solve this problem, if setDaemon (True), the main thread ends and all child threads end, as before.

If setDaemon (False) is used, the main thread waits for the end of the thread, which is equivalent to calling the join method of the thread.

Therefore, if you annotate and modify the preceding setDaemon to True, the program will become an endless loop.

In fact, we do not recommend the above practice. The above practice is a bit of a thread pool taste, but if you have seen some python thread pool implementations, while True

Loop check and exit statements are certainly available, because in the python world, pythonic is more explicit than concealed. But unfortunately, the above Code comes

Self-developed and <writing high-quality code: 91 suggestions for improving Python Programs> I did not spray this book, but I think the code example is open to discussion.

You may wonder how setDaemon (False) is equivalent to thread join ?, Don't worry, and listen to me.

This problem persists. The threading module introduces the _ MainThread object.

# Special thread class to represent the main thread# This is garbage collected through an exit handlerclass _MainThread(Thread):    def __init__(self):        Thread.__init__(self, name="MainThread")        self._Thread__started.set()        self._set_ident()        with _active_limbo_lock:            _active[_get_ident()] = self    def _set_daemon(self):        return False    def _exitfunc(self):        self._Thread__stop()        t = _pickSomeNonDaemonThread()        if t:            if __debug__:                self._note("%s: waiting for other threads", self)        while t:            t.join()            t = _pickSomeNonDaemonThread()        if __debug__:            self._note("%s: exiting", self)        self._Thread__delete()def _pickSomeNonDaemonThread():    for t in enumerate():        if not t.daemon and t.is_alive():            return t    return None# Create the main thread object,# and make it available for the interpreter# (Py_Main) as threading._shutdown._shutdown = _MainThread()._exitfunc
In fact, _ MainThread does not do anything. The only contribution is that an instance is created during the threading module import and the _ exitfunc
Assign a value to the _ shutdown function. _ Exitfunc collects all non-daemon and alive threads and calls the join method of the thread. Oh, it turns out to be

_ MainThread is quietly working behind the scenes. who calls the _ shutdown function?

It will be called before python destroys the runtime, so when you open pythonrun. c, you will find the following function

/* Wait until threading._shutdown completes, provided   the threading module was imported in the first place.   The shutdown routine will wait until all non-daemon   "threading" threads have completed. */static voidwait_for_thread_shutdown(void){#ifdef WITH_THREAD    PyObject *result;    PyThreadState *tstate = PyThreadState_GET();    PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,                                                  "threading");    if (threading == NULL) {        /* threading not imported */        PyErr_Clear();        return;    }    result = PyObject_CallMethod(threading, "_shutdown", "");    if (result == NULL)        PyErr_WriteUnraisable(threading);    else        Py_DECREF(result);    Py_DECREF(threading);#endif}
It turns out that this guy is playing tricks and is gaining insights. It turns out that there is still a need to call The py code in C. No way. Who makes the threading module pure py?
Code !!!




Thread termination in python Programming

Def _ exitCheckfunc (): print "OK" try: while 1: alive = False if thread _. isAlive (): alive = True if not alive: break time. sleep (1) # To make the statistical time run, capture KeyboardInterrupt: ctrl-c timing t KeyboardInterrupt, e: traceback. print_exc () print "consume time:", time. time ()-start threading. _ shutdown = _ exitCheckfunc write an endless loop in the main thread to receive the signal of ctrl + c.
Or use Process Monitoring:
Code.activestate.com/..-prog/


In the python threading module, after multiple threads are generated, how does one obtain the string returned after the thread is executed?

Multi-threaded/multi-process communication or callback, rather than directly returning results. This is easy to understand, because if you use the returned results to assign values to a variable, you must wait until the function ends and your program will be blocked, this eliminates the significance of multi-threaded/multi-process blocking.
Communication can be event-driven or transmitted using thread-safe data structures (such as Queue, 0mq, and rabbitMQ ), callback means that after a program is executed, you can call another function to process the next step.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.