Python Learning notes-thread threading

Source: Internet
Author: User

1 Thread Threading1.1 Basic Overview

Also known as the lightweight process.

Threads are a way of multitasking with computers, and you can use your computer's multicore resources.

The smallest unit of work in a thread-dead application

1.2 Threading Features

(1) Process creation cost is large, thread creation overhead is small

(2) A process can contain multiple threads

(3) thread is attached to the existence of a process, and multiple threads share the resources of the process

(4) Each thread also has its own unique characteristics, such as ID, instruction set

Attention:

(1) The process has independent space, the data security is high

(2) thread uses the resources of the process, that is, the general use of global variables in the way of inter-thread communication, it requires more complex synchronous mutex

(3) Multiple unrelated threading features it's best not to get together and form a thread

2 Threading Module

Thread module emulating a subset of Java ' s threading model

2.1 Threading modules and the like

Thread,event, Lock, Rlock, Condition, [Bounded]semaphore, Timer, Local.

2.2 Methods of threading modules

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 ()).

2.3 Constants of the threading module

Threading. Timeout_max sets the threading global time-out.

3 Thread class3.1 Thread class creation method

The methods of Class Thread (builtins.object) defined here:

Thread (self, group=none, Target=none, Name=none, args= (), Kwargs=none, *, Daemon=none)

Group: Thread groups, which are not implemented at this time, are set up to implement the extension of the Threadgroup class in the future.

Target: The object to execute the method called by the Run () method

Name: The name of the thread, by default, named Thread-n (N is the smallest decimal number)

Args: Tuple parameters

Kwargs: Dictionary Parameters

3.2 Instance methods for the thread class:

Start (): Start thread

Join (self, timeout=none): Waits for the thread to end, blocks the thread until the thread calling this method terminates or arrives at the specified timeout

Is_alive (): Returns True if the thread is running;

IsAlive: Equivalent to Is_alive ()

GetName (): Gets the thread name

SetName (): Sets the name of the thread in threading. The method is not available in the Help documentation for Thread (), and there are other materials on it.

Is/setdaemon (BOOL): Gets/sets is a background thread (the default foreground thread (False)). (set before Start)

If it is a background thread, during the main thread execution, the background thread is also in progress, and after the main thread executes, the main thread and the background thread will stop, regardless of the success or not.
If it is the foreground thread, during the main thread execution, the foreground thread is also in progress, and after the main thread finishes executing, wait for the foreground thread to finish, the program stops
3.3

Python Learning notes-thread threading

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.