About multi-threading in Python

Source: Internet
Author: User
This article mainly introduces Python multi-threaded instance of the relevant information, the need for friends can refer to the following

Python multi-threaded instance detailed

Multithreading is usually a new background thread to handle the more time-consuming operations, Python do background threading is also very simple, today from the official documents found a demo.

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 Foregr Ound. ')  Background.join ()  # Wait for the background task to finish print (' Main program waited until background is done. ')

Results:

the Main program continues to run in foreground. Finished background zip Of:mydata.txt Main program waited until background is done. Press any key to continue ... 

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.