This example describes the threading Hyper-threading usage in Python. Share to everyone for your reference. The specific analysis is as follows:
Threading Java-based threading model design. Lock and condition variables (Condition) are the basic behavior of objects in Java (each object has its own lock and condition variable), whereas in Python it is a separate object. The Python thread provides a subset of the behavior of Java thread, no priority, no thread group, and no thread can be stopped, paused, resumed, or interrupted. Some of the static methods implemented by Python in Java thread are provided in the form of a module method in threading.
Common methods provided by the threading module:
Threading.currentthread (): Returns the current thread variable.
Threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends.
Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).
Classes provided by the threading module:
Thread, Lock, Rlock, Condition, [Bounded]semaphore, Event, Timer, Local.
Thread is a threading class, similar to Java, there are two ways of using it, passing directly to the method to be run or inheriting from thread and overwriting run ():
# Encoding:utf-8import threading# Method 1: The method to be executed as a parameter passed to thread's constructor def func (): print ' func () passed to Thread ' t = Threadi Ng. Thread (Target=func) T.start () # Method 2: Inherits from thread and overrides the Run () class MyThread (threading. Thread): def run (self): print ' MyThread extended from Thread ' t = MyThread () T.start ()
Construction Method:
Thread (Group=none, Target=none, Name=none, args= (), kwargs={})
Group: Thread groups, currently not implemented, the library reference must be none;
Target: The method to be executed;
Name: thread name;
Args/kwargs: The parameter to pass in the method.
Example method:
IsAlive (): Returns whether the thread is running. Running refers to after startup, before terminating.
Get/setname (name): Gets/sets the thread name.
Is/setdaemon (BOOL): Gets/sets whether the daemon thread. The initial value inherits from the thread that created the thread. The program terminates when no non-daemon thread is still running.
Start (): Starts the thread.
Join ([timeout]): Blocks the thread of the current context until the thread calling this method terminates or arrives at the specified timeout (optional parameter).
An example of using join ():
# Encoding:utf-8import Threadingimport timedef context (tjoin): print ' in Threadcontext. ' Tjoin.start () # will block Tcontext until Threadjoin terminates. Tjoin.join () # Tjoin continues execution after termination. print ' Out Threadcontext. ' Def join (): print ' in Threadjoin. ' Time.sleep (1) print ' Out threadjoin. ' Tjoin = Threading. Thread (target=join) Tcontext = Threading. Thread (Target=context, args= (Tjoin,)) Tcontext.start ()
Operation Result:
In Threadcontext.
In Threadjoin.
Out Threadjoin.
Out Threadcontext.
Hopefully this article will help you with Python programming.