html experiment a

Anchor jump

Creation process

## introduction process (theory part)

1. The difference between the process and procedures

Process is a program run by the CPU, program the file on disk

2. introduce a few concepts

Serial: All of a process of a settlement by the cpu.

Concurrent: single cpu, simultaneously executing a plurality of processes (switching back and forth), appears to run simultaneously.

Parallel: multiple cpu, real running multiple processes simultaneously.

Obstruction: IO encountered called obstruction.

A cpu to run two processes, a process in which there is no obstruction,

Non-blocking: no IO.

Creation process

What is more open process: socket: server, client two processes.

python, if you want to open a number of processes, must be a primary process, open multiple sub-processes.

linux, windows: open a child process from the main process:

** same point: ** principles: the main process of open space in the child two independent processes are isolated from each other, independently of each other.

**difference**:

linux: initial data space of the child process is entirely a copy from the main (parent) process.

windows: the initial data space of the child process is entirely from the main (parent) process a copy, but different.

 

 

 


## process module introduction

process module is a module process of creating, by means of this module, you can complete the creation process.

The object of this class is instantiated obtained indicates that the task a sub-process (not yet started)

Emphasize:

1. The need to use keywords to specify the parameter
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

 

Process approach

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

Process attributes

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 () before setting the p.start

Name of the process: 2. p.name

pid process: 3. p.pid

4. p.exitcode: processes running to None, if -N, representing the end signal N (can 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

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.

 

## Python as many concurrent processes

Two ways to start a process of

`` `
# Import from multiprocessing Process
# Import Time
#
#
# DEF Task (name):
#
# Print (F '{name} running IS')
# the time.sleep (. 3)
# Print (F '{name} IS DONE ')
#
#
__main__ # IF __name__ ==' ': under # windows environment, open multi-process must be placed below the
#
# = the p-process (target = Task, args = (' hate brother ',)) # args must be in the form of a tuple.
# p.start ()
# # notify the operating system, you have given me to open up a space in memory, p will go into this process, and then let the cpu execute.
# Print ( '=== primary process' )

 

# The second way to learn
from multiprocessing Import Process
Import Time


class MyProcess(Process):

__init __ DEF (Self, name):
Super () .__ the init __ () # must inherit the parent class __init__

self.name = name

def run (self): # name must be defined run.

print(f'{self.name} is running')
time.sleep(3)
print(f'{self.name} is done')


'__Main__' if __name__ ==: at # windows environment, open multi-process must be placed below this

p = MyProcess ( 'hate hate Brother')
p.start ()
# notify the operating system, you have given me to open up a space in memory, this process will p into them, and then let the cpu execution.
Print ( 'primary process === ')
`

Jump top anchor

 

Guess you like

Origin www.cnblogs.com/Jacob-yang/p/11318345.html