Python's processes, threads, and threading modules

Source: Internet
Author: User
Tags mutex semaphore

(Note: This article is part of the content from the Internet, due to the author's limited, shortcomings, but also hope to comment.) )

Miss in school, I accidentally touched the wrong, the teacher will say: You are wrong, and I always stubborn to be correct for themselves. My heart is disdain, until in the face of Truth bow.

After graduation, sometimes in fact is going to enter the wrong (or just into a step), but he is aware of the time. Ask everybody: That what thing I did wrong, how to rescue a bit. "No, you're right." At this moment, my heart is a sense of urgency and helplessness. Until... Until... Kneel before the wrong.

Gee, life is nasty, but it's okay, luckily I'm witty. I am me, the man who cooks with a digger.

Okay, here we go, show time.

In the Python world has been an ancient legend, that is, Python multithreading is chicken, then the legend of the credibility of how much? If our code is CPU intensive (involving a lot of computations), the code of multiple threads is likely to be executed linearly, so in this case multithreading is chicken, efficiency may not be as single-threaded as there is a context Switch (in fact, the switch between threads and the creation of threads is time consuming), but: if it is io-intensive, multithreading can significantly improve efficiency. For example, the most time crawler is waiting for the socket to return data. At this point the C code has the release Gil, and the end result is that another thread can continue executing while the thread waits for IO.

First, the process

The program does not run alone, and the program is assigned resources to run only if it is loaded into memory, and this execution is called a process. The difference between a program and a process is that a program is a set of instructions, which is a static descriptive text of the process, and a process is an execution activity of a program, which belongs to the dynamic concept. The procedure in progress is the process.

Second, the thread

A thread, sometimes called a lightweight process, is the smallest unit of a program's execution flow (the smallest dispatch unit of the operating system). Threads belong to the process, we usually write simple program, is single-threaded, multi-threaded and single-threaded difference is that multithreading can handle multiple tasks at the same time.

Third, the co-process

A process is a lightweight thread that is user-state. If multiple processes are multi-CPU and multithreading corresponds to multi-core CPUs, then event-driven and co-scheduling is fully exploiting the potential of a single-core CPU that continually improves performance. We can take advantage of the asynchronous advantage, but also avoid repeated system calls, as well as the cost of process switching, which is the association. The co-process is also single-threaded, but it allows non-human code to be written using asynchronous + callback methods, which can be written in a seemingly synchronous way. It is the key to the so-called non-preemptive collaboration to achieve push-pull interactions. For Python, because a python multi-threaded global interpreter causes only one thread to access the CPU at the same time, the demand for the coprocessor is more urgent than other languages.

Iv. relationship of process, thread and association

IO-intensive applications: Multi-process, multithreading---event-driven

CPU-intensive applications: multi-process and multi-threading

Scheduling and switching time: Process > Threads > co-processes

On the relationship between process and thread, there is a good explanation on the Internet:

1. The core of the computer is the CPU, which takes on all the computational tasks. It's like a factory that's always running.

2. Assuming that the plant has limited power, it can only supply one workshop at a time. In other words, when a workshop is started, other workshops must be shut down. The implication behind this is that

A single CPU can run only one task at a time.

3. The process is like a factory workshop, which represents a single task that the CPU can handle. At any one time, the CPU always runs a process and the other processes are not running.

4. In a workshop, there can be many workers. They work together to complete a task. A thread is like a worker in a shop. A process can include multiple threads.

5. The workshop space is shared by the workers, for example, many rooms are accessible to every worker. This symbolizes that the memory space of a process is shared and can be used by each thread

These shared memory.

6. However, the size of each room is different, and some rooms can accommodate a maximum of one person, such as a toilet. When there are people in it, the others can't go in. This represents a thread

When using some shared memory, other threads must wait for it to end before they can use this piece of memory.

7. A simple way to prevent others from entering is to add a lock to the door. The first person to lock the door, after the people see locked, in the doorway line, wait for the lock to open and then go in. This is

called "Mutex lock" (Mutual exclusion, abbreviated MUTEX) to prevent multiple threads from reading and writing to a chunk of memory at the same time.

8. There are also rooms that can accommodate n individuals, such as kitchens. In other words, if the number of people is greater than N, the extra person can only wait outside. This is like some memory area,

Only a fixed number of threads can be used.

9. The solution at this point is to hang n the key at the door. The person who goes in takes a key and hangs the key back when he comes out. After the man found the key overhead, he knew

must wait in line at the door. This is called a "semaphore"(Semaphore), which guarantees that multiple threads do not conflict with each other.

10. It is not difficult to see that a mutex is a special case of Semaphore (N=1). That is, it is entirely possible to replace the former with the latter. However, because the mutex is relatively simple,

and high efficiency, so in the case of the need to ensure the monopoly of resources, or the use of this design.

11. The operating system is designed so it can be summed up to three points:

(1) Multi-process mode, allowing multiple tasks to run simultaneously;

(2) in multi-threaded form, allowing individual tasks to be divided into different parts of the operation;

(3) Provide a coordination mechanism to prevent conflicts between processes and threads, and to allow the sharing of resources between processes and threads on the other.

V. Introduction of Threading Module

Threading is used to provide thread-related operations, which are the smallest unit of work in an application.

ImportThreading#Import ModuleImport TimedefShow (ARG): Time.sleep (3)    Print(ARG)if __name__=='__main__':  forIinchRange (10): T= Threading. Thread (target=show,args= (i,))#target is the function return value, the args after the input variable, the last variable to add,T.start ()#Start Thread#10 Threads started

· Start thread is ready to wait for CPU scheduling

· SetName setting a name for a thread

· GetName Get thread Name

· Setdaemon set to background thread or foreground thread (default)
If it is a background thread, during the main thread execution, the background thread is also in progress, and after the main thread finishes executing, the background thread stops regardless of 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

· The join executes each thread one by one and continues execution after execution, making multithreading meaningless

· The Run method that executes the thread object automatically after the run thread is dispatched by the CPU

To join the main thread:

ImportThreading#Import ModuleImport TimedefShow (ARG):Print('hello%d'%Arg) time.sleep (3)    Print(ARG)if __name__=='__main__':  forIinchRange (10): T= Threading. Thread (target=show,args= (i,))#target is the function return value, the args after the input variable, the last variable to add,T.start ()#Start Thread#10 Child threads are startedPrint('Qwer')#Main Thread

Operation Result:

After the child thread is started, the main thread is directly executed (instead of executing the main thread when the child thread finishes executing).

If you want the sub-thread to finish executing and then perform the main thread, you need to add the Jion function.

ImportThreading#Import ModuleImportTimel1= []defShow (ARG):Print('hello%d'%Arg) time.sleep (3)    Print(ARG)if __name__=='__main__':  forIinchRange (10): T= Threading. Thread (target=show,args= (i,))#target is the function return value, the args after the input variable, the last variable to add,T.start ()#Start Threadl1.append (t) forTinchL1:t.join ()#wait for each thread to finish executing#10 Threads startedPrint('Qwer')

introduce the daemon thread Deamon:

ImportThreading#Import ModuleImport TimedefShow (ARG):Print('hello%d'%Arg) time.sleep (3)    Print(ARG)if __name__=='__main__':  forIinchRange (10): T= Threading. Thread (target=show,args= (i,))#target is the function return value, the args after the input variable, the last variable to add,T.setdaemon (True)#Daemon, exit when the main thread finishes executingT.start ()#Start Thread#10 Threads startedPrint('Qwer')

Operation Result:

After the main thread executes, the child threads are stopped, and after sleep in the show function is not executed.

Six, the thread lock (like Python3, I have to confirm it)

Because there is a random dispatch between threads, and each thread may execute only N, dirty data may appear when multiple threads modify the same piece of data at the same time, so a thread lock is present-allowing one of the threads to perform the operation at the same time.

Import TimeImportThreadingdefaddnum ():GlobalNum#get this global variable in each thread    Print('--get Num:', num) time.sleep (1) Num-=1#perform a-1 operation on this common variablenum = 100#set a shared variableThread_list = [] forIinchRange (100): T= Threading. Thread (target=addnum) T.start () thread_list.append (t) forTinchThread_list:#wait for all threads to finish executingT.join ()Print('Final num:', num)

Normally, this num result should be 0, but in Python 2.7 run more than a few times, you will find that the final printout of the NUM result is not always 0, why each run the result is different? Ha, very simple, if you have a A, a, two threads, at this time to the NUM to reduce the 1 operation, because 2 threads are concurrently running concurrently, so 2 threads are likely to take the num=100 this initial variable to the CPU to calculate, when a thread to end the result is 99, But at this point the result of the B-thread operation is also 99, two threads at the same time the result of the CPU operation is assigned to the NUM variable, the result is 99. What about that? It is very simple that each thread will modify the data in order to avoid having to modify it when it is not finished, so you can add a lock to the data so that other threads will have to wait for you to modify the data and release the lock before accessing the data.

Say how the multi-process on the quad-core CPU is going:

For quad-core CPUs, if four threads hit four cores and start at the same time but cannot execute, the Gil lock must be performed. So only one thread can be executed at the same time. Purpose of locking: A thread is not executed by multiple CPUs.

Popular: Four cores equivalent to four toilets, four workers A, B, C, D (threads) entered four toilets respectively, they want to urinate, but in order to shy (avoid multiple executions), need to add a lock on the door (Gil Lock, the system has only one lock). A lock, start urinating, pee for 2 seconds (about 100 lines of CPU on the bottom of the command); give B,b a start to urinate (this time a, C, D hold). And then give C, and then D ... Therefore, Python can only execute one thread at a time.

If A and B threads belong to the same process, there is a possibility of an error. For example, for loop i=i+1 (initial is 0),

If a assigns I equal to 0 to register, has not finished, the lock time is up, then B obtains the I value is still 0, just executes in the lock time, the value of I becomes 1. Then lock to a, and the value in the register is still 0, and the result is still 1. Executed two times, the result is still 1. (Python3.0 above has solved this problem.) )

Well, just write this.

National Day eight days, all at home holding the computer extricate oneself,

It is also a very flattered.

Python's processes, threads, and threading modules

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.