Created
July 30, 2018 23:07
-
-
Save chrismcfee/0d318ee1a0f138aa693ec77f64c61b4b to your computer and use it in GitHub Desktop.
multithread.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading, zipfile, time | |
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() | |
time.sleep(5) | |
print 'Finished background zip of: ', self.infile | |
background = AsyncZip('mydata.txt', 'myarchive.zip') | |
background.start() | |
print 'The main program continues to run in foreground.' | |
#time.sleep(5) | |
background.join() # Wait for the background task to finish | |
print 'Main program waited until background was done.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment