Example Analysis of threading hyperthreading usage in python, pythonthreading

Source: Internet
Author: User

Example Analysis of threading hyperthreading usage in python, pythonthreading

This example describes how to use threading hyperthreading in python. Share it with you for your reference. The specific analysis is as follows:

Threading is a Java-based thread model design. Lock and Condition variables are the basic behavior of objects in Java (each object has a Lock and Condition variable), while in Python, they are independent objects. Python Thread provides a subset of Java Thread behaviors. Without a priority or Thread group, the Thread cannot be stopped, paused, recovered, or interrupted. Some static methods implemented by Python in Java Thread are provided in threading in the form of module methods.

Common methods provided by the threading module:

Threading. currentThread (): returns the current thread variable.
Threading. enumerate (): returns a list containing running threads. Running means that the threads before and after startup and termination are not included.
Threading. activeCount (): returns the number of running threads, with the same results as len (threading. enumerate.

Classes provided by the threading module:

Thread, Lock, Rlock, Condition, [Bounded] Semaphore, Event, Timer, local.

Thread is a Thread class. Similar to Java, there are two ways to use it: directly pass in the method to be run or inherit from Thread and overwrite run ():

# Encoding: UTF-8import threading # Method 1: The method to be executed is passed to the Thread's constructor def func (): print 'func () passed to thread' t = threading. thread (target = func) t. start () # Method 2: Inherit from Thread and override run () class MyThread (threading. thread): def run (self): print 'mythread extended from thread' t = MyThread () t. start ()

Constructor:

Thread (group = None, target = None, name = None, args = (), kwargs = {})
Group: thread group, which has not been implemented yet. The database reference prompts that it must be None;
Target: method to be executed;
Name: Specifies the thread name;
Args/kwargs: parameter of the method to be passed in.

Instance method:

IsAlive (): returns whether the thread is running. Running means starting and before termination.
Get/setName (name): get/set the thread name.
Is/setDaemon (bool): gets/sets whether to guard the thread. The initial value is inherited from the thread that created the thread. When no non-daemon thread is still running, the program will be terminated.
Start (): start the thread.
Join ([timeout]): blocks the thread in the current context until the thread that calls this method terminates or reaches the specified timeout (optional parameter ).

An example of using join:

# Encoding: UTF-8import threadingimport timedef context (tJoin): print 'in threadContext. 'tJoin. start () # Blocks tContext until threadJoin ends. TJoin. join () # tJoin continues 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 ()

Running result:

In threadContext.
In threadJoin.
Out threadJoin.
Out threadContext.

I hope this article will help you with Python programming.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.