python--Threads

Source: Internet
Author: User
Tags switches thread class

Thread

Threads belong to lightweight processes

Note: A process is the smallest unit of resource allocation, and a thread is the smallest unit of CPU scheduling. there is at least one thread in each process. relationship of processes and threads

 

the difference between a thread and a processCan be summed up in the following 4 points: 1) address space and other resources (such as open files): Processes are independent of each other and shared among threads of the same process.  Threads within a process are not visible in other processes.  2) Communication: Inter-process communication IPC, between threads can directly read and write process data segments (such as global variables) to communicate-the need for process synchronization and mutual exclusion means of support to ensure data consistency.  3) Scheduling and switching: Thread context switches are much faster than process context switches. 4) in a multithreaded operating system, the process is not an executable entity. features of the thread

1) Light body

Entities in a thread do not have system resources at all, but have a bit of an essential resource that can be run independently. The entities of the thread include programs, data, and TCB. A thread is a dynamic concept, and its dynamic nature is described by the thread control block TCB.

2) The basic unit of independent Dispatch and dispatch.

In multi-threaded OS, threads are the basic units that can run independently, and thus are the basic units of independent Dispatch and dispatch. Because the thread is "light", the thread switches very quickly and with little overhead (in the same process).

3) Share process resources.

Threads in the same process can share the resources owned by the process, which first manifests that all threads have the same process ID, which means that the thread can access every memory resource of the process, and also access the open files, timers, semaphores, etc. owned by the process. Because threads in the same process share memory and files, there is no need to call the kernel to communicate with one another.

4) can be executed concurrently.

In a process between multiple threads, can be executed concurrently, and even allow all threads in a process to execute concurrently, also, the threads in different processes can execute concurrently, take full advantage of and play the processor and peripheral equipment to work in parallel.

Global interpreter Lock Gil

The execution of the Python code is controlled by the Python virtual machine (also known as the interpreter main loop). Python was designed to take into account the main loop, while only one thread was executing. Although the Python interpreter can "run" multiple threads, only one thread runs in the interpreter at any time.
Access to the Python virtual machine is controlled by the global Interpreter Lock (GIL), which ensures that only one thread is running at the same time.

In a multithreaded environment, the Python virtual machine executes as follows:

A, set GIL;

b, switch to a thread to run;

C, run a specified number of bytecode instructions or threads actively give up control (can call Time.sleep (0));

D, set the thread to sleep state;

e, unlock GIL;

D. Repeat all of the above steps again.
The Gil will be locked until the function ends (because no Python bytecode is running, so there is no thread switching) when calling external code (such as C + + extension functions), programmers who write extensions can actively unlock the Gil.

Threading Module Thread Creation Threading.thread class
 fromThreadingImportThreadImport TimedefSayhi (name): Time.sleep (2)    Print('%s Say hello'%name)if __name__=='__main__': T=thread (target=sayhi,args= ('Egon',)) T.start ()Print('Main Thread')
Creating Threads

python--Threads

Related Article

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.