Concepts of processes and threads
Processes and threads are two very important concepts in the operating system, and for a typical program, there may be several processes, each of which has several concurrently executing threads. A process is the smallest unit of resource management, and a thread is the smallest unit of program execution (threads can share all the resources in the same process, and the processes are independent).
Process (Progress)
Intuitively, a process is an executing program (each of the. py files in Python can be seen as a process), a basic unit in a multitasking operating system, and a collection of program directives and related resources.
The operating system isolates the address space that each process can access, and if you want to pass information between processes, you can use interprocess communication or files, databases, and so on. (Communication between processes in Windows http://www.360doc.com/content/14/0802/18/3218170_398864589.shtml)
In the process of scheduling, the transition between processes takes a lot of time, in order to better utilize the resource sharing and cost savings, the concept of threading evolved from the process.
Thread (threading)
A thread is the execution unit of a process, and for most programs, there is only one main thread, and in order to be more efficient, some programs use multi-threading, and all threads in the system appear to be executing concurrently.
In Python, if I/O intensive tasks can use multi-threading, it is not recommended to use multi-threading for computationally intensive tasks(the best way is to write directly in C)
GIL
Global interpreter lock, which is a computer programming language interpreter (available only in the CPython interpreter), is used to synchronize threads so that only one thread executes at any moment, and the Python thread is the native thread of the operating system.
On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Even on multi-core CPUs
On the platform, because of the existence of Gil, multi-threaded parallel execution is forbidden.
File Execution order
Create a simple thread
Non-threaded threads
ImportThreading fromTimeImportCtime,sleepdefMusic (func):Print("start....%s----%s"%(Func,ctime ())) Sleep (4) Print("end.....%s----%s"%(Func,ctime ()))defMove (func):Print("start.....%s----%s"%(Func,ctime ())) Sleep (8) Print("end......%s----%s"%(Func,ctime ())) T1= Threading. Thread (target=music,args= ("Music",)) T2= Threading. Thread (target=move,args= ("movie",))if __name__=="__main__": T1.start () T2.start ()Print("aaaaaaaaaaaaaaa%s"%CTime ())" "execution Result: start .... Music----Thu Sep 15:56:47 2016start ..... Movie----Thu Sep 15:56:47 2016aaaaaaaaaaaaaaaThu Sep 15:56:47 2016end ..... Music----Thu Sep 15:56:51 2016end ... Movie----Thu Sep 15:56:55" " Simple Threaded Threads
Python Learning notes----threads, processes