Python supports multithreading primarily through the two modules of thread and threading. Python's thread module is the lower-level module, Python's threading module is the thread to do some encapsulation, can be more convenient to use. But Python (CPython) cannot use threading to make full use of CPU resources due to the existence of the Gil, and multiprocessing modules are required if you want to maximize the computational power of multicore CPUs.
Consider using stackless python if you have high requirements for threading applications. Stackless Python is a modified version of Python and has better support for multithreaded programming, providing support for micro-threading. Micro-Threading is a lightweight thread that takes more time to switch between multiple threads and consumes less resources.
There are two ways to create a new thread through the threading module: one is through threading. Thread (Target=executable method)-that is, one executable (or object) passed to the thread object, and the second is the inheritance of threading. Thread defines the subclass and overrides the Run () method. In the second method, the only method that must be overridden is run (), which, if necessary, determines whether to override __init__ (). It is important to note that to override __init__ (), the parent class's __init__ () must be called on the first line of the function, otherwise the error "assertionerror:thread.__init__ () not called" will be triggered
The Python threading module differs from other languages in that it does not provide a thread termination method through Python threading. Thread () threads that start are independent of each other, and a and B are threads that run independently of each other if thread B is started in threads a. If you want to terminate thread a while forcefully terminating thread B, an easy way is to implement it by calling B.setdaemon (True) in threads A. However, the problem is that the resources in thread B (open files, data transfers, and so on) may not be released correctly. So Setdaemon () is not a good way, and the more appropriate way is through the event mechanism. The following procedure shows the difference between the Setdaemon () and the event mechanism terminating the child threads.
Import threading Import Time Class Mythread (threading. Thread): def __init__ (self,stopevt = None,file=none,name = ' Subthread ', Type = ' event '): Threading. Thread.__init__ (self) self.stopevt = stopevt Self.name = name self. File = File self. Type = Type Def eventrun (self): When not Self.stopevt.isSet (): Print Self.name + ' alive\n ' Time.sleep (2) if self. File:print ' Close opened file in ' +self.name+ ' \ n ' self. File.close () Print Self.name + ' stoped\n ' def Daemonrun (self): D = Mythreaddaemon (self. File) D.setdaemon (True) while not Self.stopevt.isSet (): Print Self.name + ' alive\n ' Time.sleep (2) Print Self.name + ' stoped\n ' def run: if self. Type = = ' event ': self. Eventrun () else:self. Daemonrun () class MythreadDaemon (Threading. Thread): def __init__ (self,file=none,name = ' Daemonthread '): Threading. Thread.__init__ (self) self.name = name Self. File = File def run (self): while true:print self.name + ' alive\n ' Time.sleep ( 2) if self. File:print ' Close opened file in ' +self.name+ ' \ n ' self. File.close () Print Self.name + ' stoped\n ' def evtstop (): Stopevt = Threading. Event () FileA = open (' TestA.txt ', ' w ') Fileb = open (' TestB.txt ', ' w ') A = Mythread (Stopevt,filea, ' Subthrea DA ') B = Mythread (Stopevt,fileb, ' subthreadb ') print repr (Threading.currentthread ()) + ' alive\n ' Print File A.name + ' closed? ' +repr (filea.closed) + ' \ n ' Print Fileb.name + ' closed? ' +repr (fileb.closed) + ' \ n ' a.start () B.start () time.sleep (1) Print repr (Threading.currentthread ()) + ' Send Stop signal\n ' Stopevt.set () a.join() B.join () Print repr (Threading.currentthread ()) + ' stoped\n ' print ' after A stoped, ' +filea.name + ' cl Osed? ' +repr (filea.closed) + ' \ n ' print ' after A stoped, ' +fileb.name + ' closed? ' +repr (fileb.closed) + ' \ n ' def daemonstop (): Stopevt = Threading. Event () FileA = open (' TestA.txt ', ' r ') A = Mythread (Stopevt,filea, ' Subthreada ', Type = ' Daemon ') print rep R (Threading.currentthread ()) + ' alive\n ' Print Filea.name + ' closed? ' +repr (filea.closed) + ' \ n ' a.start () time.sleep (1) stopevt.set () a.join () Print repr (Threadi Ng.currentthread ()) + ' stoped\n ' print ' after A stoped, ' +filea.name + ' closed? ' +repr (filea.closed) + ' \ n ' if not FileA.closed:print ' I see the differents, the resource in Subthread MA Y not released with Setdaemon () ' Filea.close () if __name__ = = ' __main__ ': print '-------stop Subthread EX Ample with Event:----------\ n ' evtstop () print '-------Daemon Stop Subthread Example:----------\ n ' daemonstop ()
The operating result is:
-------Stop Subthread example with Event:---------- <_mainthread (Mainthread, started 2436) >alive TestA.txt closed? False testB.txt closed? False Subthreada Alive subthreadb Alive <_mainthread (Mainthread, started 2436) >send stop signal Close opened file in Subthreada close opened file in subthreadb subthreada stoped subthreadb STOPED
<_mainthread (Mainthread, started 2436) >stoped after A stoped, TestA.txt closed? True after A stoped, TestB.txt closed? True -------Daemon Stop subthread Example:---------- <_mainthread (Mainthread, started 2436) >alive TestA.txt closed? False Subthreada Alive subthreada stoped <_mainthread (Mainthread, started 2436) >stoped After A stoped, TestA.txt closed? False you see the differents, the resource in Subthread may not released with Setdaemon ()
Python Multi thread creation and termination