Python Learning notes----threads, processes

Source: Internet
Author: User

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

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.