-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.py
28 lines (20 loc) · 831 Bytes
/
thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import threading, zipfile
import os
class AsyncZip(threading.Thread):
def __init__(self, infile, outfile):
threading.Thread.__init__(self)
self.infile = infile
self.outfile = outfile
def run(self):
print("zipping file in the background")
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
print('Finished creating zip', self.outfile)
os.system("touch pydata.txt")
bgThread = AsyncZip('pydata.txt', 'pyarchive.zip')
bgThread.start()
print('The main program continues to run in foreground.')
bgThread.join() # Wait for the background task to finish
# Better design: concentrate all access to a resource in a single thread and
# then use the Queue module to feed that thread with requests from other threads.