The thread class of the Theading module
Property:
Name Thread Name
Ident thread identifiers
Daemon Boolean value indicating whether it is a daemon thread
Method:
__init__ (Target=none, Name=none, *args= (), **kwargs={})
Start () execution thread
Run () method to define threading functionality
Join (Timeout=none) blocks threads, waits for wake-up, better than busy waiting
There are three main ways to use the thread class:
1. Create a thread instance and pass it to one of its parameters
2. Create a thread instance and pass it to a callable class instance
3. Deriving a thread subclass, creating a subclass instance
Here are three ways to use:
1. Create a thread instance and pass it to one of its parameters
1 fromThreadingImportThread2 fromTimeImportCTime, Sleep3 4loops = [4, 2]5 6 7 defLoop (Nloop, nsec):8 Print('Loops', Nloop,'starting at:', CTime ())9 Sleep (nsec)Ten Print('Loop', Nloop,'end at:', CTime ()) One A - defMain (): - Print('starting at:', CTime ()) theThreads = [] - - forIinchRange (len (loops)): -t = Thread (Target=loop, args=(I,loops[i])) + Threads.append (t) # # #保存类实例 - + forIinchRange (len (loops)): A Threads[i].start () # # #同时启动类实例 at - forIinchRange (len (loops)): - Threads[i].join () # # #保持主线程切出 - - Print('All do at :', CTime ()) - in if __name__=='__main__': -Main ()
Operation Result:
Starting At:mon Dec 19 23:29:58 2016
Loops 0 Starting At:mon Dec 19 23:29:58 2016
Loops 1 Starting At:mon Dec 19 23:29:58 2016
Loop 1 End At:mon Dec 19 23:30:00 2016
Loop 0 End At:mon Dec 19 23:30:02 2016
All done At:mon Dec 19 23:30:02 2016
Method 1, the incoming function creates an instance of the class without manually setting the lock, releasing the lock
Method Two: Pass in the callable class to create the class instance (usage feels a bit like an adorner)
1 fromThreadingImportThread2 fromTimeImportCTime, Sleep3 4loops = [4, 2]5 6 7 classTfun ():8 def __init__(self, name, Func, args):9Self.name =nameTenSelf.func =func OneSelf.args =args A - def __call__(self): - returnSelf.func (*Self.args) the - - defLoop (Nloop, nsec): - Print('Loop', Nloop,'At :', CTime ()) + Sleep (nsec) - Print('Loop', Nloop,'At :', CTime ()) + A at defMain (): - Print('starting at:', CTime ()) -Threads = [] - - forIinchRange (len (loops)): -t = Thread (Target=tfun (Loop).__name__, Loop, (I,loops[i] ))) in threads.append (t) - to forIinchRange (len (loops)): + Threads[i].start () - the forIinchRange (len (loops)): * Threads[i].join () $ Panax Notoginseng Print('All do at :', CTime ()) - the if __name__=='__main__': +Main ()
Operation Result:
Starting At:mon Dec 19 23:46:31 2016
Loop 0 At:mon Dec 19 23:46:31 2016
Loop 1 At:mon Dec 19 23:46:31 2016
Loop 1 At:mon Dec 19 23:46:33 2016
Loop 0 At:mon Dec 19 23:46:35 2016
All done At:mon Dec 19 23:46:35 2016
Method Three:
Derived thread subclasses, creating subclass instances
1 fromThreadingImportThread2 fromTimeImportCTime, Sleep3 4loops = [4, 2]5 6 7 classMythread (Thread):8 def __init__(Self, func, args, name="'):9Thread.__init__(self)TenSelf.name =name OneSelf.func =func ASelf.args =args -Self.name =name - the defRun (self):#Note not __call__ (self) -Self.func (*Self.args) - - + defLoop (Nloop, nsec): - Print('Loop', Nloop,'At :', CTime ()) + Sleep (nsec) A Print('Loop', Nloop,'end at:', CTime ()) at - - defMain (): - Print('starting at:', CTime ()) -Threads = [] - in forIinchRange (len (loops)): -t =Mythread (Loop, (I,loops[i])) to threads.append (t) + - forIinchRange (len (loops)): the Threads[i].start () * $ forIinchRange (len (loops)):Panax Notoginseng Threads[i].join () - the Print('end at:', CTime ()) + A if __name__=='__main__': theMain ()
Operation Result:
Starting At:tue Dec 20 00:06:00 2016
Loop 0 At:tue Dec 20 00:06:00 2016
Loop 1 At:tue Dec 20 00:06:00 2016
Loop 1 End at:tue Dec 20 00:06:02 2016
Loop 0 End At:tue Dec 20 00:06:04 2016
End At:tue Dec 20 00:06:04 2016
Method Two and method three can test several functions, in which the method three is more intuitive. But Method 1 is much simpler
"[email protected]" threading module