python syntax basis - Concurrent Programming - the process - the process theory and open process

##############################################

"""
Concurrent programming concepts:

process
1, the operation of the program, is the process, the program was lifeless entity, run up there is life, and
The operating system can manage the process, the basic process is the operating system of execution units,
2, each process has its own address space between processes is not mixed, such as micro-channel qq can not access the address space,
Isolated from the operating system for you, and this is the reason the process of introducing the concept of the operating system,

#######################################
Scheduling process
1, first come, first served, there is a bad, is not conducive to short job
2, short operating priority algorithm, but its bad for a long job; can 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 algorithm is executed in turn, has a very scientific,
4, the multi-level feedback queue algorithm, a plurality of queues, there is a new task into the first queue, which is combined with round-robin priority, the second task to place the next one,

#######################################
Concurrent and Parallel:
Parallel processes: this can be achieved only in a multi-core cpu,
Concurrent processes: This is a turn to perform, due to the very fast, looks like executed together, such as listening to music again, again writing code,

######################################
Three-state transition diagram of the process: very important
1, when a process starts running and is ready state, this is the first state, is to tell the cpu, I'm ready to run, and to enter the queuing,
2, round-robin, then it's your turn, you run, this is the second state,
3, the occurrence of obstruction, this is the third state, such as the program allows you to enter your content, input method, this time it is blocked, after you enter the finished, it has a smooth,
This is waiting for I / O completion, the input and output input, sleep, file,
After the event handler, you have to enter the ready state,
All processing is over, over,

###########################################
Synchronous and asynchronous
1, synchronized, to wait, to wait in line, you are nobody capable,
2, asynchronous, no need to wait, you can do other things,

###########################################
Blocking and non-blocking
1, obstruction, is input, sleep these need to wait, this is blocked,
2, non-blocking, blocking is skipped, but the program needs to block the inevitable, because of the need to wait for content processing,

###########################################
Synchronous and asynchronous non-blocking blocking:
Synchronous blocking, that is,
Synchronous non-blocking
Blocking asynchronous
Asynchronous non-blocking, more efficient,

"""

 

##############################################

"""
Python using multiple processes

Running program is a process. All the processes are by its parent to create.
Therefore, running the python program is a process, then we can re-create the process in the program.
Multiple concurrent processes can achieve the effect, it will allow faster execution speed of the program.

There is a built-in multi-process module, we need to use this module:
from multiprocessing import Process

"""

 

##############################################

"""
multiprocessing module
In this package it includes almost all sub-modules and related processes. Because so many sub-module, in order to facilitate classification of memory,
I this part of the roughly divided into four parts:
1, part of the creation process,
2, process synchronization section,
3, part of the process pool,
4, sharing data between processes.

multiprocessing.Process Introduction

Parameter Description
#############
Process ([group [, target [, name [, args [, kwargs]]]]]), obtained by the object instantiated, showing a sub-process task (not yet started)
Emphasize:
1. The need to use keywords to specify the parameters
2. args specified target position for the transfer function parameters, is in the form of a tuple, there must be a comma
Parameter Description:
1 group parameter is not used, the values ​​are always None
2 target represents the calling object, task child process that is to be executed
3 args parameter indicates the position of the tuple call object, args = (1,2, 'egon',)
4 kwargs call object in the dictionary, kwargs = { 'name': 'egon', 'age': 18}
5 name is the name of the child process

Methods Introduction
############
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,
Using 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 it should be emphasized that, p.join can join live start open process, and can not join the live run open process

Introduction property
###############
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,
p can not create their own new process must be set up before the p.start ()
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
"""

 

#################### way to start the process 1 ######################### ###

Create a single process and view the main process id, the parent process id, the child process id,

Note passing parameters, args is to pass a tuple, the elements of time to have a comma, kargs is to pass a dictionary,

from multiprocessing import Process
import time
import the


def task(name):
    print('{} is running!'.format(name)) print ( 'sub-process id:', os.getpid (), ' parent process ID:' , os.getppid ()) the time.sleep (. 3 ) print ( '!} {IS DONE' .format (name)) # Windows open in the child process to write __name __ == __ main__ # under open because the child process will re-load the content if the parent process == __name__ '__main__' : Print ( 'primary process id:', Os.getpid ()) # Create a Python in the process object p = Process (target = task, args = ( 't1',)) # registration, which is the main process # p = Process (target = task, kwargs = { 'name': 't1'}) p.start () # call the operating system interface to start a process execution command, which is a sub-process, # is asynchronous between the child and the primary process now, if I want the child process after the re-execute the following code into sync, how to do? p.join () # # This is to join in a perception of a child process ends, will change asynchronous synchronization, # print ( "parent process ID of the parent process", os.getppid ()) # This is the process ID of pycharm , print ( '--- ---- primary process') # add a join, this one will only execute "" "no plus join after the end of all the sub-processes: the primary process id: 8560 --- main process ---- t1 is running child process id:! 9528 the parent process id:! 8560 t1 is done add the join: the main process id: 8592 t1 is running child process id:! 6448 the parent process id: 8592 t1 is done! --- ---- the main process life cycle process # # the main process does not turn the child process is finished execute his code on the end of the # child process is executing the code yourself over, opened the sub-# process of the main process, the main process execution is over, then they have to wait for the end of the child process, the main process can end, "" "

Creating multiple processes:

from multiprocessing import Process
import time


def task(name):
    print('{} is running!'.format(name))
    time.sleep(3) Print ( '{} IS DONE!' .format (name)) IF the __name__ == '__main__' : sub-process # 10 open = P_LIST [] for I in Range (10 ): Process P = (target = Task, args = (i,)) p_list.append (p ) p.start () # p.join () [p.join () for p in p_list] # previous 10 process to ensure that all is over, will execute the following code , print ( '--- ---- primary process' ) # this opens up multiple processes, multiple processes can be read to save the file, take the file contents,

 

#######################    startup mode process 2        ###################### ###

# 2 way to start the process
# First, create a class that inherits process
# Second, the class must implement the run method, this method run inside the child process is to be performed,
import the
from multiprocessing import Process

class MyProcess (Process): # import succession process,
    def __init __ (self, name): # can pass parameters to the process,
        super () .__ init __ () # This is the inheritance of all the parameters of the parent class,
        self.name=name
    def run(self):
        # print(os.getpid()) print ( "No child processes" , self.pid) print ( "parameters", self.name) # print (os.getpid ()) These two are the same,

 the __name__ == IF '__main__' : p1 = MyProcess # () # p1 which is not pass parameter = MyProcess ( "name1") # This parameter is passed, which is the object-oriented instantiated, p2 = MyProcess ( 'name2 ' ) p3 = MyProcess (' NAME3 ' ) p1.start () will automatically call #start RUN p2.start () # p2.run () p3.start () # between the three processes is asynchronous, p1.join () p2.join () p3.join () # three processes are executed until the end of the following, which is the asynchronous, become asynchronous, print ( 'main thread')

 

Data is isolated between ############### ############## process

Data between processes isolate the problem #
Whether the data between the process and the process is # isolation, such data between the micro-channel qq and is isolated,
# Between several processes, if not through special means, it is impossible to share the data, remember this, nothing intelligible,

# The following is think of a way to prove this conclusion, but the conclusion you remember on the line,
from multiprocessing import Process

def work():
    global n # declare a global variable,
    n=0
    print ( 'the sub-process:', n)


if __name__ == '__main__':
    n = 100
    p=Process(target=work)
    p.start()
    print ( 'the main process:', n)

 

 

###############################################

################################################

Guess you like

Origin www.cnblogs.com/andy0816/p/12375624.html