First, TarPackage Packaging
ImportTarfileImportOSdefFuntarfile (tfname): TF= Tarfile.open (Tfname,'W:gz')#Open opens a tar package, ' W ' open Mode for write ': GZ ' compression mode gzip forFNameinchOs.listdir ('.'):#traverse the current directory file ifFname.endswith ('. docx'): Tf.add (fname)#Add This file to the tar packageOs.remove (fname)#remove this filetf.close ()Print(tf.members)#Print Tar package member information if notTf.members:#determine if the tar package is emptyOs.remove (Tfname)#if the tar package is empty, delete the tar packageFuntarfile ('docxtar.tgz')
Output Result:
' 8-1 \xc8\xe7\xba\xce\xca\xb9\xd3\xc3\xb6\xe0\xcf\xdf\xb3\xcc-\xb8\xb1\xb1\xbe.docx ' ' 8-2 \xc8\xe7\xba\xce\xcf\xdf\xb3\xcc\xbc\xe4\xcd\xa8\xd0\xc5-\xb8\xb1\xb1\xbe.docx ' ' 8-3 \xc8\xe7\xba\xce\xcf\xdf\xb3\xcc\xbc\xe4\xca\xc2\xbc\xfe\xcd\xa8\xd6\xaa-\xb8\xb1\xb1\xbe.docx ' at 0x2602d30>]
second, how to use the event notification:
>>> fromThreadingImportEvent,thread>>> >>>defF (E):Print('F 0') e.wait ()#event waiting, blocking Print('F 1') >>> e =Event ()>>> t = Thread (target=f, args= (E,))#Create a child thread, run the F function>>> T.start ()#Child Threads RunF 0#Child threads blocking to event waits>>>E.set ()#Mainline Thread event sendF 1#Child threads continue to execute>>>
When the wait () block is called again , it will no longer work, and you need to call e.clear () cleanup before you can use wait () blocking
#-*-coding:cp936-*- fromThreadingImportthread,event fromQueueImportQueue fromTimeImportSleep"""Download the thread, download the download number and write the data to the queue after downloading"""classDownthread (Thread):def __init__(self,sid,queue):#thread.__init__ (self)Super (Downthread,self).__init__() Self.sid=Sid Self.queue=QueuedefdownLoad (self,sid):Print("Download (%d) ..."%SID) Sleep (2) defRun (self): self.download (SELF.SID) data= self.sid+100Self.queue.put ((self.sid,data))"""convert thread, read data from the queue for file conversion, number of changes after a certain number of events notify the packaging thread, this thread paused"""classConvelthread (Thread):def __init__(self,queue,tevent,cevent):#thread.__init__ (self)Super (Convelthread,self).__init__() Self.queue=Queue Self.tevent=tevent self.cevent=cEvent Self.count=0defConvel (self,sid,data):Print("Convel (%d)-(%d)"%(SID, Data))defRun (self): while(True): SID, Data= Self.queue.get ()#get the data in the form of the tuple unpacking if(SID = =-1): ifSelf.count! = 0:#when you exit to determine if you still need to package,Self.tEvent.set ()#If you need to send packaging eventsSelf.cEvent.wait ()#and wait until the package is finished and then quit. Break if(data): Self.convel (sid,data) Self.count+ = 1ifSelf.count = = 5: Self.tEvent.set ()#Send Packaging EventsSelf.cEvent.wait ()#wait for conversion eventsSelf.tEvent.clear ()#Emptying packaging EventsSelf.count =0"""package threads, wait until the conversion thread notifies, start packaging, notify the conversion thread to continue the conversion after packaging is complete, and the thread suspends packaging"""classTarthread (Thread):def __init__(self,tevent,cevent):#thread.__init__ (self)Super (Tarthread,self).__init__() Self.count=0 self.tevent=tevent self.cevent=cEvent Self.setdaemon (True)#The daemon thread is set, and when all other threads end, this thread also ends defFuntar (self,count):Print('Tar count-%d'%count)defRun (self): whileTrue:self.tEvent.wait ()#Special Packaging Event notificationSelf.count + = 1Self . Funtar (Self.count) self.tEvent.clear ()#Emptying packaging EventsSelf.cEvent.set ()#Send Conversion Events if __name__=='__main__': Q=Queue () tevent=Event () cEvent=Event () dthreads= [Downthread (i,q) forIinchXrange (1,13)] Cthread=Convelthread (q,tevent,cevent) TThread=Tarthread (tevent,cevent) forTinchDthreads:t.start () Cthread.start () Tthread.start ( ) forTinchdthreads:t.join () q.put (-1, None)) Cthread.join ()#Tthread.join () #有此代码时, do not print the following, because the main thread waits for the conversion thread to exit, the conversion thread is the daemon thread and requires all the threads to exit before exitingPrint('Mainthread')
Output Result:
Download (1) ... Download (4) ... Download (2) ... Download (5) ... Download (3) ... Download (6) ... Download (7) ... Download (8) ... Download (9) ... Download (10) ... Download (11) ... Download (...) Convel (1)-(101) Convel (4)-(104) Convel (2)-(102) Convel (5)-( convel (3)-(103-1convel (6)-(106) Convel (7)-(107 ) Convel (8)-(108) Convel (9)-(109) Convel (ten- 2Convel (one)-(111) Convel (a)-(---3MainThread
8-3 how inter-thread event notifications