Python basis 10.0: concurrent programming process -----

A basic knowledge of the process of

1, the theoretical basics

As the name implies, a process that is a process being executed. Process is an abstraction of a running program. 

The concept originated in the process of the operating system, is the core concept of the operating system, one of the oldest and most important abstraction is the operating system. All other operating systems are all around the concept of the process of unfolding. 

PS: Even if you can only use a cpu (early computer is true), but also to ensure the ability to support the (pseudo) concurrently. Will become a separate multiple virtual cpu cpu (multi-channel technologies: time division multiplexing and spatial multiplexing + support isolated on hardware), there is no process of abstraction, modern computers will cease to exist. 

  ( 1 ) the role of the operating system: 
    hide ugly complex hardware interface, providing good abstract interface 
    management, scheduling processes, and multiple processes competing hardware becomes ordered 

  ( 2 ) multi-channel technology: 
  Background: For single nuclear, concurrency 
       now hosts are generally multi-core, each core will use multi-channel technology 
       with four cpu, io encountered obstruction to run a program cpu1, and io will wait until the end of the re-scheduling, it is scheduled to 4 one 
       of any of a cpu, as determined by the operating system scheduling algorithm. 
  Multiplexing on the space: in memory at the same time as a number of Road program 
  multiplexed on time: multiplexing a cpu time slice 
        stressed: encountered io cut, also cut occupied cpu time is too long, the core is the status of the process before the cut preserved, so 
        as to ensure the next time you switch back, can continue to operate based on the last go-cut position
Theoretical basics

2. What is the process

  Process (Process) is a computer program run on a set of data on the activities of the system is the basic unit of resource allocation and scheduling, is the underlying operating system architecture. In the computer architecture for the early design process, the basic process is the execution of the program entity; 

in contemporary computer architecture for the design of the thread, the thread is the process vessel. Program is instruction, organization of data and its description of the process is a solid program. 

  Narrow definition: the process is an instance of a program running (AN instance of A Computer Program that IS being the Executed). 

  Broadly defined: a process is run activities must have a separate function on a data collection program. It is the basic unit operating system dynamically performed in traditional operating systems, the process is both a basic allocation unit, is the basic unit of execution.
What is the process

Concept 1), the process

First, the process is an entity. Each process has its own address space, under normal circumstances, including the text area (text region), the data region (data region) and stack (stack region). Code is executed by a processor stored text area; dynamically allocated memory area stores data used during execution process variables; stack area stores instructions and local variables of the calling procedure activities.
Second, the process is an "execution procedures." Program is a lifeless entity (operating system performs the) only when the processor attached to the program of life, it can become an active entity, we call for the process.
Operating system process is basic and important concepts. After the system is a multi-channel program appears to characterize the dynamics of internal systems appeared, describing a concept system activity patterns within each channel programs introduced, all multiprogramming operating systems are based on the process.

 Cause 2), the operating system introduces the concept of process

From the theoretical point of view, it is an abstract procedure is running; from the implementation point of view, is a data structure, object of the inherent law clearly depict the dynamic system, the effective management and scheduling program into the computer system main memory operation.

Feature 3), process

Dynamics: the essence of the process is the execution of a program in a multiprogramming system, the process is dynamically generated, dynamic extinction.
Concurrency: any process can be executed concurrently with other processes with
independence: the process is a basic unit can operate independently, but also the system of allocation of resources and scheduling of independent units;
asynchronous nature: due to the interaction between processes, so that the process has performing intermittent, i.e. by separate processes, the speed of forward unpredictable
structural features: the process programs, data and process control block of three parts.
Different processes can comprise a plurality of the same program: a program in a different data set constitute a different process, different results can be obtained; however, during execution, the program change can not occur.

The difference between 4), processes and procedures

Program is an ordered set of instructions and data, which does not have any operational meaning, is a static concept. The process is a process of program execution on the processor, it is a dynamic concept. A software program can be used as long-term data exist, but there is a certain process life cycle. Program is permanent, the process is temporary.

Note: Perform the same program twice, there will be two processes in the operating system, so we can run a software at the same time, each doing different things will not be confused.

3, the process of scheduling

To run multiple processes alternately, the operating system must be scheduled for these processes, the scheduling is not carried out immediately, but need to follow certain rules, thus there is a scheduling algorithm process.

1), first-come, first-served scheduling algorithm

First-come, first-served (FCFS) scheduling algorithm is a simple scheduling algorithm for job scheduling can also be used for process scheduling. FCFS algorithm is more conducive to a long job (process), to the detriment of short job (process). It can be seen that the algorithm is suitable for CPU busy type work, to the detriment of heavy I / O type of job (process).

2), short operating priority scheduling algorithm

Short job (the process) scheduling algorithm and (SJ / PF) refers to short or short process priority job scheduling algorithm for job scheduling can also be used to schedule processes. But its work for long; not guarantee the urgency of the job (process) to be in time; the length of the job is only to be estimated.

3), round-robin method

Round-robin (Round Robin, RR) method basic idea is to have each process in the ready queue waiting time and enjoy the service proportional to the time. In the round-robin method, the processing time of the CPU time slice needs to be divided into a fixed size, e.g., several tens of milliseconds to hundreds of milliseconds. If a process run out of time slice prescribed system after being selected schedule, but not required to complete the task, it is the release of their self-occupied CPU and discharged at the end of the ready queue, waiting for the next scheduling. At the same time, the process scheduler went Scheduling a process currently in the queue ready. 
Clearly, round-robin scheduling can only be used to allocate some resources can be preempted. These resources can be preempted at any time be deprived, and they can be reassigned to another process. A CPU is to seize resources. However, printers and other resources can not be preempted. Because the job scheduling assignment to all the system hardware resources, in addition to the CPU, which comprises a non-preemptive resources, it does not use rotation job scheduling method. 
In the rotation method, the length of the time slice selected is important. First, select the length of the time slice will directly affect the response time and system overhead. If the chip length is too short, the scheduler increases the number of processors preemption. This will make the process context switches also greatly increased, thereby increasing the system overhead. Conversely, if the time slice selected length is too long, for example, to ensure that a time slice execution time required for the longest queue in the ready process can be executed, into the round-robin method a first come first served. Chip length is selected to determine the system response time requirements of the ready queue and the maximum number allowed in accordance with processes. 
In the round robin, adding to the process ready queue are three cases: 
one is to divide its time slice is used up, but the process has not been completed, return to the end of the ready queue waiting for the next scheduled execution to continue. 
Another case is the time slice given to the process is not exhausted, the request simply because the I / O synchronous relationship or mutually exclusive since the process is blocked. After unblocking return to the ready queue. 
The third case is that the new process is created to enter the ready queue.
If the difference between these treatment processes, giving different priorities and time slice from a visual point of view, the system can further improve service quality and efficiency. For example, we may be the ready queue in accordance with the process of arriving at the reasons for blocking the ready queue and the type of process is blocked into different ready queues, each arranged according to the principle of FCFS, process among different priority queues enjoy, but at the same the same priority queue. In this way, when a process after the completion of the implementation of its time slice, or wake up from sleep and being created, enter different ready queue.
Round-robin method

4), multi-level feedback queue

As a variety of process scheduling algorithm described earlier have certain limitations. Such as short process priority scheduling algorithm, only to take care of the short process while ignoring the long process, but did not specify if the length of the process, the short process priority-based preemptive scheduling algorithm and the length of the process will not be used. 
The multi-level feedback queue scheduling algorithm is not necessary to know in advance the time required to perform a variety of processes, but also to meet the needs of various types of processes, so it is now being recognized as a better process scheduling algorithm. In systems that employ multiple feedback queue scheduling algorithm, the scheduling algorithm implementation process is as follows. 
( 1) a plurality of ready queues to be provided, and given different priority for each queue. The first has the highest priority, followed by a second queue of queues, each queue priority remaining decreased by one. The algorithm gives each queue process execution time slice size can vary, the higher the priority queue, the execution time specified for each sheet the less process. For example, the second queue time slices than a first time slice twice as long as the queue, ......, + the i . 1 queue than the time slice of the i-th time slice twice as long queues. 
( 2 ) When a new process into the memory, it is first placed into the first end of the queue, waiting to be scheduled according to the principle of FCFS queuing. When it came time to perform the process, as it can be completed within the time slice, you can prepare evacuation system; if it is not completed at the end of a time slice, the process scheduler then transferred to a second end of the queue, and then Likewise waiting to be scheduled according to the principle of FCFS performed; if it is running in a second time slot queue is not yet completed, then turn it into a third queue, ......, and so on, when a long job (process) from after a queue of n sequentially down queue, the queue will be taken at the n-th time run round-robin. 

(3) only when the first queue is empty, the process scheduler dispatches a second run queue; only if the first 1 ~ (i-1) when the queue was empty, the scheduler will process the i-th run queue. If the processor is the i-th process in the queue for a service, but also a new process into the high priority queue (first to (any queue i-1)), the new process at this time will preempt running the processor processes, i.e., the process by the scheduler is running back into the end of the queue i, allocated to the processor to process the new arriving high priority.
Multilevel feedback queue

4 concurrent parallel with the process of

Parallel  : Parallel refers both to perform, such as race, two people are kept ahead of him; (enough resources, such as three threads, quad-core CPU)

Concurrency  : Concurrent refers to a limited resources, both alternately turns using resources, such as a road (monocytes CPU resources) while only one person had, after a period of A to go, give B, B continue to run A, used interchangeably, it aims to improve the efficiency.

Difference :

Parallel is microscopically, i.e. at a precise moment of time, in different program execution, which requires the existence of a plurality of processors.
Concurrency is from the macro, in a period of time can be seen to be performed simultaneously, such as a server process multiple session.

5, blocking synchronous asynchronous non-blocking

1), the state of the process of introduction

 

Before understanding other concepts, we first need to understand a few state of the process. The program is running, since the scheduling algorithm is the operating system of control, the program will enter several states: ready, running and blocking.

  (1) Ready (Ready) state

  When the process has been assigned to all the necessary resources in addition to the CPU, as long as the processor can get immediate execution, when the process state called the ready state.

  (2) execution / operation (Running) state is obtained when the process processors, which program is executed on a processor, a process at this time is referred to as a state execution state.

  (3) blocked (Blocked) state process is being executed, due to waiting for an event can not be executed, and they give up the processor is blocked. Cause the process to block a variety of events, such as waiting for I / O completion, the application does not meet the buffer, wait for a letter (signal) and so on.

 2), synchronous and asynchronous

所谓同步就是一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能算完成,这是一种可靠的任务序列. Either succeed are successful, failed and failed, two state of the task can be consistent.

  所谓异步是不需要等待被依赖的任务完成,只是通知被依赖的任务要完成什么工作,依赖的任务也立即执行,只要自己完成了整个任务就算完成了. As a final task was dependent on whether truly complete, depending on its mission can not be determined 所以它是不可靠的任务序列.

example:

For example, I go to the bank to conduct business, there may be two ways:

The first: Choose to wait in line; second: choose my number to take a small piece of paper on top of, informed me it was my turn to go through the business when I was discharged until the counter by the number of people;

The first: the former (waiting in line) is waiting for the synchronization message notification, that is, I've been waiting for bank business conditions;

The second: the latter (waiting for others to notice) is waiting for asynchronous message notification. In asynchronous message processing, the message waiting notification persons (in this case is to wait for people to conduct business) tend to register a callback mechanism when waiting for the event to be triggered by a trigger mechanism (in this case, the counter person) by some mechanism (in this case, write the number on a small piece of paper, call number) to find people waiting for the event.

3), blocking and non-blocking

Blocking and non-blocking state when these two concepts and procedures (thread) to wait for message notification (does not matter synchronous or asynchronous) related. That is the main blocking and non-blocking state when the angle of the program (thread) to wait for the message notification

example:

Continue the example above, use either queuing or waiting notification number, if in the waiting process, in addition to those waiting outside to wait for a message notification can not do other things, then the mechanism is blocked, the performance in the program, also the program is unable to continue down the implementation has been blocked at the call of the function.
On the contrary, some people go through times like these business in the bank while playing call and send text messages while waiting, such a state is non-blocking, because he (waiter) is not blocked on the message notification, but while doing their own things while waiting.

Note: Non-blocking synchronous form is actually inefficient, imagine that while the name of the phone while you also need to look up in the end the team routed you do not. If the phone line and the observation position as two operation program, then the program needs between these two different behaviors of the switching back and forth, it can be imagined efficiency is low; but asynchronously form nonblocking no such problem, because calling you (waiters) thing, and you notice is the counter (news trigger mechanism) things, the program does not switch back and forth in two different operations.

4), the synchronous / asynchronous with the blocking / nonblocking

  1. Synchronous blocking form

  Minimum efficiency. Take the example above, it is to concentrate on your line, do not do anything else.

  1. Blocking asynchronous form

  If the bank waiting for a man of business 采用的是异步的方式去等待消息被触发(通知), which is picking up a piece of paper, if he can not leave the bank to do something else during that time, then it is clear that this person is blocked in this operating above waiting;

  异步操作是可以被阻塞住的,只不过它不是在处理消息时阻塞,而是在等待消息通知时被阻塞。

  1. Synchronous non-blocking form

  It is actually inefficient.

  Imagine that while the name of the phone while you also need to look up in the end the team routed you do not, if the call queuing and observation positions as two programs operate, then 这个程序需要在这两种不同的行为之间来回的切换, imagine the efficiency is low.

  1. Asynchronous non-blocking form

  higher efficiency,

  Because you are calling (waiters) thing, and you notice is the counter (news trigger mechanism) things 程序没有在两种不同的操作中来回切换.

  For example, this person suddenly find themselves guilty of addiction, need to go out a cigarette, so he told the lobby manager, said this number when I discharged the trouble to inform me out, then he would not be blocked waiting for this operation above, this is the natural way of non-blocking asynchronous +.

Many people will be confused and block synchronization are 因为很多时候同步操作会以阻塞的形式表现出来the same, many people will confuse the asynchronous and non-blocking, 因为异步操作一般都不会在真正的IO操作处被阻塞.

6, the end of the process of creation

Created 1), the process

But all the hardware, the operating system needs to have to manage, as long as the concept of an operating system, there is a process, you need to have a way to create a process, some operating systems designed for only one application, such as a microwave oven controller, once started microwave, all processes already exist.

  As for the general system (running many applications), you need to have the ability to create or revocation process during system operation, mainly in the form is divided into 4 create a new process:

  1. system initialization (see the process with ps command linux, windows with Task Manager, foreground process responsible for interacting with users, processes running in the background regardless of the user, runs in the background and only when needed wake-up process, called daemons, such as e-mail, web pages, news, print)

  2. A process opens a child process during operation (such as nginx open multi-process, os.fork, subprocess.Popen, etc.)

  3. The user interactive requests, and create a new process (such as the user double-STORM)

  4. initialize a batch job (only applications in a mainframe batch system)

  Either way, create a new process is the implementation of a system for creating process is called by an existing process created.

End 2), process

  1. Normal exit (voluntary, if the user clicks crosses interactive page, or call the procedure is finished to initiate normal exit system call, with the exit linux, in windows using ExitProcess)

  2. Error exit (voluntary, python a.py in a.py does not exist)

  3. serious error (involuntary, to execute an illegal instruction, such as references to nonexistent memory, 1/0, etc., you can catch the exception, try ... except ...)

  4. killed by another process (involuntary, such as kill -9)

Two, multiprocess module

1, multiprocess.process module

Created 1), the process

Process ([group [, target [, name [, args [, kwargs]]]]]), obtained by the object instantiated, showing a sub-process task (not yet started)

He stressed:
1. The need to use the keyword way to specify parameters
2. args specified for the target location parameter passed to the function, is a tuple form, there must be a comma

Parameter Description:
Group parameter is not used, the value is always None
showing call object target, i.e., sub-tasks to execute the process
args parameter indicates the position of the tuple call object, args = (1,2, 'Egon',)
kwargs call object represents dictionary, kwargs = { 'name': 'Egon', 'Age':} 18 is
name of the child name

2) The method described

1 p.start (): to start the process, and call the child process p.run ()

2 p.run (): method of operation, when the process starts, it is the function to call the specified target, we custom classes in this method must be achieved

3 p.terminate (): p forcibly terminate the process, will not carry out any cleaning operation, if p create a child process, the child process to become a zombie process, this method requires special care this situation. If p then also saved a lock will not be released, leading to a deadlock

4 p.is_alive (): If p is still running and returns True

5 p.join ([timeout]): main thread wait terminated p (emphasized: in a state of the main thread and the like, and p is in the running state). timeout is optional timeout should be emphasized that, p.join can join live start open process, and can not join the live run open process

3), property description

1 p.daemon: The default value is False, if set to True, the representative of p is a daemon running in the background, when the termination of the parent process p, p also will be terminated, and after setting True, you can not create your own p the new process must be p.start () before setting

Name of the process: 2 p.name

pid process: 3 p.pid

4 p.exitcode: process at runtime to None, if -N, representing the end of the signal N (to understand)

5 p.authkey: identity verification key process, by default os.urandom 32 randomly generated character string (). To succeed (to understand) when the key purpose is to provide security for the network connection between the underlying processes involved in communication, this type of connection only with the same authentication key

4) use of process modules in the windows Notes

In the Windows operating system, because there is no fork (the process of creating a mechanism for the linux operating system), when creating the child process will automatically import this file to start it, but when they perform in the import of the entire file. So if the process () directly written in the file will create a child process infinite recursion error. It is necessary to create a child process of partially used if __name__ == '__ main__' judgment protected, import time, it will not run recursively.

Guess you like

Origin www.cnblogs.com/CatdeXin/p/11040139.html