python's GIL lock

Process: A program system is running, it is the basic unit of resource allocation system.

Thread: is the smallest unit of the operation is performed in the process, is the basic unit processor scheduling.

Processor: storing computer programs and data, and an instruction execution unit in accordance with a predetermined program steps. It comprises a central processor, a main memory, I / O interfaces.

Program: the processor program is a complete description of a task instruction sequence.

Instructions: the processor can interpret, direct execution information unit.

Computer system: + peripheral device handler.

Parallel: a plurality of simultaneously executing a plurality of tasks cpu, suppose there are two programs both programs run simultaneously on two different cpu.

Concurrency: single cpu alternately perform multiple tasks, assuming that there are two programs, both alternately run on the cpu, instead of running at the same time, just because of the time of execution is too fast, people mistakenly thought it was in the "simultaneous" . Competition has executed depends on the time slice resources. Two of synchronous and concurrent relationships are mutually exclusive.

Sync: do not mean at the same time , refers to the thread between a certain order be run, end of the current thread, the next thread to be able to run.

Asynchronous: the thread can not run simultaneously.

(1) a thread can belong to only one process, and a process can have multiple threads, but there is at least one thread. Is the minimum thread execution units and the scheduling of the operating system recognizes.
(2) the resources allocated to the process, all threads in the same process of sharing all the resources of the process. The same process multiple threads share code segments (code and constant), the data segment (global variables and static variables), the extended segment (heap memory). But each thread has its own stack segment, stack segment called running time, used to store all local variables and temporary variables.
(3) points to the thread handling machine, i.e. the real processors running on a thread.
(4) threads in the implementation process, the need to work in sync. Between threads of different processes to make use of synchronized way messaging.
If the class of the process compared to the process, then each student is a thread, they share the classroom, that is, memory space threads share the process. Each time, only one student asked the teacher questions, teacher answer is completed, the next turn. That thread owns the cpu in a time slice.

 

GIL exist in C language interpreter written in python, python language itself does not exist the GIL. GIL is cpython the global interpreter lock, and only one. When the thread run is completed, other threads to run multiple threads running the same process, a thread when running python program will take up the Python interpreter (ie get GIL), the other threads within that process can not run, etc. . Even in case the multi-core, can only play to the performance of a single core.

When the lock is released?

(1) for i / o threads of cpu is not required, i.e. cpu idle.

(2) cpu intensive thread, cpu occupied will always be calculated, this time by an instruction counter, when the (100) one thread is executing a certain number of instructions, the GIL will be released after the release of a plurality of threads GIL to compete.

GIL is the solution?

(1) when using multiple threads, the use of other languages;

(2) change a Python interpreter;

(3) the use of multiple processes;

GIL and mutex What is the difference?

Mutex: orderly change when modifying data, without confusion data.

 Photo resolved Note: Performing a certain order between threads is not mutually exclusive, but can only execute one thread at a time.

Guess you like

Origin www.cnblogs.com/xiximayou/p/11729788.html