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 ...