Python multi-thread programming (8): Merge threads and background threads

Source: Internet
Author: User

[From] nickname: Holbrook http://www.cnblogs.com/holbrook/archive/2012/02/25/2368231.html

Merge threads

The join () method is also provided in the Thread class of python, so that a Thread can continue to run after the execution of another Thread ends. This method can also set a timeout parameter to avoid endless waiting. Because the two threads are completed in sequence and look like a thread, it is called thread merging. Example:

import threading
import random
import time

class MyThread(threading.Thread):

def run(self):
wait_time=random.randrange(1,10)
print "%s will wait %d seconds" % (self.name, wait_time)
time.sleep(wait_time)
print "%s finished!" % self.name

if __name__=="__main__":
threads = []
for i in range(5):
t = MyThread()
t.start()
threads.append(t)
print 'main thread is waitting for exit...'
for t in threads:
t.join(1)

print 'main thread finished!'

Execution result:

Thread-1 will wait 3 seconds
Thread-2 will wait 4 seconds
Thread-3 will wait 1 seconds
Thread-4 will wait 5 seconds
Thread-5 will wait 3 seconds
Main thread is waitting for exit...
Thread-3 finished!
Thread-1 finished!
Thread-5 finished!
Main thread finished!
Thread-2 finished!
Thread-4 finished!

Threads with too long sleep time (2 and 4 here) will not be waiting.

Background thread

By default, when the main thread exits, it waits for the end of all sub-threads. If you want the main thread to automatically end all the sub-threads when exiting without waiting for the sub-thread, you need to set the sub-thread as the background thread (daemon ). The method is to call the setDaemon () method of the thread class. As follows:

import threading
import random
import time

class MyThread(threading.Thread):

def run(self):
wait_time=random.randrange(1,10)
print "%s will wait %d seconds" % (self.name, wait_time)
time.sleep(wait_time)
print "%s finished!" % self.name

if __name__=="__main__":
print 'main thread is waitting for exit...'

for i in range(5):
t = MyThread()
t.setDaemon(True)
t.start()

print 'main thread finished!'

Execution result:

Main thread is waitting for exit...
Thread-1 will wait 3 seconds
Thread-2 will wait 3 seconds
Thread-3 will wait 4 seconds
Thread-4 will wait 7 seconds
Thread-5 will wait 7 seconds
Main thread finished!

It can be seen that the main thread does not wait for the sub-thread to execute, but directly exits.

Summary

The join () method allows the thread to wait for another thread to run, while the setDaemon () method does not wait for the Child thread at the end. Both join and setDaemon can change the running sequence between threads.

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.