Multiple Threads in python and multiple threads in python

Source: Internet
Author: User

Multiple Threads in python and multiple threads in python
Send a monkey rescue

 

When I used python multithreading today, I found that I don't know how to properly wait for all threads to end before the main thread ends.

In fact, at the end of the day, I realized that this was all worrying. The instance from Thread () was originally ended after the main process was completed.

Official explanation:

daemon A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when no alive non-daemon threads are left.

The default value of daemon is false, and it will not be blocked after start (nonsense, otherwise multithreading is required, but it is always possible to block the join operation ). it's perfect for my needs. In fact, I don't need to do anything.

You can use setDaemon (True) to change the value of daemon. In this case, you need to call join to wait until the subthread ends. (This is a good move ..)

For t in threads: t. join ()

You can put all the threads to be started into a queue and join each thread. This can be done, but it looks ugly. It is really awkward to use.

As follows, in a for loop, the process waits for the first join:

#!/usr/bin/env python# -*- coding: utf-8 -*-from threading import Threadimport timedef target(i):    time.sleep(10-i)def main():    threads = []    for i in range(10):        t = Thread(target=target,args=(i,))        threads.append(t)    for t in threads:        t.start()    for idx,t in enumerate(threads):        print idx        t.join()if __name__ == '__main__':    main()
Multiprocessing. Pool

I implemented it again with Queue, but it is still too ugly to encapsulate well. Then I found that Python actually has a multiprocessing. Pool, which can be nice to implement:

#!/usr/bin/env python# -*- coding: utf-8 -*-from multiprocessing import Poolimport timedef work():    print time.time()    time.sleep(4)def main():    pool = Pool(processes=4)    for i in range(4):        pool.apply_async(work)    pool.close()    pool.join()if __name__ == '__main__':    main()
Use the official Pool to implement the simplest Multiprocessing Pool.
#! /Usr/bin/env python #-*-coding: UTF-8-*-from threading import Threadfrom Queue import Queueimport timedef hello (msg = None): print time. time (), msg time. sleep (5) class MPool (object): def posttask (self, func): def inner (* args, ** kwargs ): q = args [0] real_args = args [1:] func (* real_args, ** kwargs) q. task_done () return inner def _ init _ (self): self. _ queue = Queue () # For simplicity, the maximum queue def run_task (self, target, * args, ** kwargs): task = Thread (target = self. posttask (target), args = (self. _ queue,) + args, kwargs = kwargs) self. _ queue. put (task) task. setDaemon (True) task. start () def join (self): self. _ queue. join () def main (): p = MPool () for I in range (10): p. run_task (hello, I) p. join () if _ name _ = '_ main _': main ()

Finally, I want to exit after all the threads are finished. This is the default, and join is useless.

Related Article

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.