An example analysis of threading Hyper-threading usage in Python

Source: Internet
Author: User
Tags instance method terminates

The example in this article describes threading Hyper-threading usage in Python. Share to everyone for your reference. The specific analysis is as follows:

Threading Java-based threading model design. Locks (lock) and condition variables (Condition) are the basic behavior of objects in Java (each object has its own lock and condition variable), while in Python it is a separate object. Python thread provides a subset of the behavior of Java thread; There is no priority, thread group, and thread cannot be stopped, paused, recovered, interrupted. Some of the Java thread's static methods implemented in Python are provided in the form of modular methods in threading.

The common methods provided by the threading module are:

Threading.currentthread (): Returns the current thread variable.

Threading.enumerate (): Returns a list containing the running thread. Running refers to threads that start and end after the thread has started, not including before and after the start.

Threading.activecount (): Returns the number of threads running, 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, with two ways of using it, passing directly to the method to run or inheriting from thread and overwriting run ():

?

1 2 3 4 5 6 7 8 9 10 11 12-13 # Encoding:utf-8 Import Threading # Method 1: The method to be executed is passed as a parameter to the construction method of Thread def func (): print ' func () passed to Thread ' t = Threadin G.thread (Target=func) T.start () # Method 2: Inherit from Thread and override 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 Group, currently not implemented, the library reference must be a hint of none;

Target: The method to be executed;

Name: thread name;

Args/kwargs: The arguments to pass in the method.

Instance method:

IsAlive (): Returns whether the thread is running. Running refers to the start, before termination.

Get/setname (name): Gets/sets the thread name.

Is/setdaemon (BOOL): Gets/Sets whether the daemon is a thread. The initial value is inherited from the thread that created the thread. When no non-daemon thread is still running, the program terminates.

Start (): Start the thread.

Join ([timeout]): The thread that blocks the current context environment until the thread calling this method terminates or arrives at the specified timeout (optional parameter).

An example of using join ():

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 # Encoding:utf-8 Import Threading Import Time Def context (Tjoin): print ' in Threadcontext. ' Tjoin.start () # will block Tcontext until Threadjoin terminated. Tjoin.join () # Tjoin continues execution after termination. print ' Out Threadcontext. ' Def join (): print ' Threadjoin. ' Time.sleep (1) print ' Out threadjoin. ' Tjoin = Threading. Thread (target=join) Tcontext = Threading. Thread (Target=context, args= (Tjoin,)) Tcontext.start ()

Run Result:

In Threadcontext.

In Threadjoin.

Out Threadjoin.

Out Threadcontext.

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.