Details about Python multi-thread instances and python multi-thread instances

Source: Internet
Author: User

Details about Python multi-thread instances and python multi-thread instances

Detailed description of Python multi-thread instances

Multithreading is usually a new background thread to process time-consuming operations. It is also very easy for Python to process Background threads. Today, I found a Demo from the official documentation.

Instance code:

import threading, zipfile  class AsyncZip(threading.Thread):   def __init__(self, infile, outfile):     threading.Thread.__init__(self)     self.infile = infile     self.outfile = outfile   def run(self):     f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)     f.write(self.infile)     f.close()     print('Finished background zip of:', self.infile)  background = AsyncZip('mydata.txt', 'myarchive.zip') background.start() print('The main program continues to run in foreground.')  background.join()  # Wait for the background task to finish print('Main program waited until background was done.') 

Result:

The main program continues to run in foreground. Finished background zip of: mydata.txt Main program waited until background was done. Press any key to continue . . . 

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.