1. Understanding the process
The concept of process:(process
)
A process is a running program,It is a part of the operating system,Smallest unit of resource allocation. Resource allocation:Allocatedcpuand physical resources such as memory The process number is a unique identifier for the process Two processes after the same program is executed twice Processes and relationships between processes: Segregation of data from each other,pass (a bill or inspection etc)socketcorrespond (by letter etc)
Parallelism and concurrency:
erupt simultaneously:ancpuConstant execution of multiple programs at the same time side by side (of two processes, developments, thoughts etc):multi- (faceted, ethnic etc)cpuConstant execution of multiple programs at the same time
Methods of process scheduling for cpu:
# first come first server fcfs (first come first server): first come first execution # Short job prioritization algorithm: allocate more cpu, first to the short finish calculation # Time Slice Rotation Algorithm: Each task executes one time slice of time. Then the others are executed. # Multi-level feedback queue algorithm The longer the,cpuThe fewer resources allocated,low priority The shorter the,cpuThe more resources allocated
Jobs 1, 2, 3, and 4 are given 0.4 seconds each, and job 1 is completed, and jobs 2, 3, and 4 are not completed, and are placed in the second level queue, placing the subsequent shorter jobs in the first level queue. Jobs in the secondary queue are given 0.3 seconds each, job 2 is completed, jobs 3 and 4 are placed in the tertiary queue and given 0.2 seconds each, job 3 is completed, job 4 is not completed, it is placed in the quaternary queue and is given 0.1 seconds to be processed, and it is possible that job 1 is a download job.
Process three-state diagram:
(1)be in order(Ready)state of affairs onlyCPUNeed to implement external,All other resources have been allocated 称为be in orderstate of affairs。 (2)fulfillment(Running)state of affairs cpu开始fulfillment该进程时称为fulfillmentstate of affairs。 (3)blockage(Blocked)state of affairs 由于wait for某个事件发生而无法fulfillment时,便是blockagestate of affairs,cpufulfillment其他进程.for example,wait forI/Ofulfillmentinput、Application buffer can't be met, etc.。
Synchronous Asynchronous / Blocking Non-blocking:
Scene in the middle of multitasking synchronization:I'll have to wait until I'm done with this.,You're doing it.,There's only one main line.,就是synchronization synchronous:I'm not done with this.,You're doing it.,There are two main lines,就是synchronous blockage:For example, the code hasinput,就是blockage,A string must be entered,Otherwise the code doesn't go down 非blockage:There's no waiting around.,Normal code runs down the line. # Synchronous blocking :inefficient, cpu underutilization # Asynchronous blocking : e.g. socketserver, you can connect to more than one at the same time, but they all have recvs. # Synchronized non-blocking: no input-like code, executed from top to bottom. The default normal code # Asynchronous non-blocking: efficiency is the highest, the cpu is overfull, overheating Liquid cooling #
Guardian Process:
# You can label a child process with a daemon name, which will end when the main process code is executed (daemon for the main process). (1)The daemon will terminate as soon as the main process has finished executing its code. (2)No more child processes can be opened within the daemon,Otherwise, an exception is thrown.(realize)
Lock:
()# Locked up ()# Unlocked # Allow one process to have one lock at a time # That's Lock. Locking ensures that when multiple processes modify the same piece of data,Only one task can be modified at a time,i.e. serial modification,rest assured!,It's slow.,But sacrificing speed ensures data security。 # Allow multiple locks on multiple processes at the same time is [semaphore] Signals are a variation of locks: The actual implementation is register + padlock,同时允许多个进程上padlock # Mutual Exclusion Lock : Mutual exclusion lock is the process of mutual exclusion, who first grabbed the resource, who will be locked to change the content of the resource, in order to ensure the synchronization of data # Note: Multiple locks together, without unlocking, will cause a deadlock. Locking and unlocking are a pair.
2. Syntax of the process
### Process process import os,time """ # ps -aux View process number # ps -aux | grep 2784 Filter for process 2784 # Force the killing of processes kill -9 process number # Get the current process number res = () print(res) # Get the parent of the current process res = () print(res) """ from multiprocessing import Process # (1) Use of processes """ def func(): # 1.child process id:3561,2.parent process id:3560 print("1.subprocessid:{},2.parent processid:{}".format((),())) if __name__ == "__main__": # Create a child process and return the process object p = Process(target=func) # Call the subprocess () # 3. master process id:3560, 4. parent process id:3327 print("3.master processid:{},4.parent processid:{}".format((),())) """ # (2) Creating processes with parameters """ def func(n): (1) for i in range(1,n+1): # 0 ~ n-1 print(i) print("1.subprocessid:{},2.parent processid:{}".format((),())) if __name__ == "__main__": n = 6 # target = specified task args = tuple of arguments p = Process(target=func , args=(n,)) () for i in range(1,n+1): print("*" * i) """ # (3) Isolation of data between processes from each other """ total = 100 def func(): global total total +=1 print(total) if __name__ == "__main__": p = Process(target=func) () (1) print(total) """ # (4) Asynchronicity between processes """ 1. multiple processes are asynchronous concurrent programs, because the cpu scheduling strategy, not necessarily the first execution of which task By default, the main process execution speed is slightly faster than the sub-process, because the sub-process creation, to allocate space resources may be blocked blocking state, the cpu will immediately switch tasks to maximize the efficiency of the overall speed of the program 2. The default master process to wait for all the child processes after the end of the execution, in the unified closure of the program, to release resources If you do not wait, the sub-processes may continue to occupy cpu and memory resources in the background of the system to form a zombie process. In order to facilitate process management, the master process waits for the child processes by default. After closing the program in a unified way. """ def func(n): print("1.subprocessid:{},2.parent processid:{}".format((),()) , n ) if __name__ == "__main__": for i in range(1,11): p = Process(target=func,args=(i,)) () print("The main process execution has ended ... " , () )
3. join customized process classes
Execute the main process after all the sub-processes have been executed.
# ### 1. Synchronize master and child processes : join """You must wait for the current child process to finish executing before executing the following code;, which is used to synchronize the child parent process;""" from multiprocessing import Process import time # (1) Basic use of join """ def func(): print("Send the first e-mail: My dear leader, are you there?") if __name__ == "__main__": p = Process(target=func) () # (0.1) () print("Send the second e-mail: I want to say, raise my salary to 60,000 a month.") """ # (2) joins in multi-process scenarios """ def func(i): (1) print("Send the first email{} : My own leader.,Are you there??".format(i)) if __name__ == "__main__": lst = [] for i in range(1,11): p = Process(target=func,args=(i,)) () # join written in will cause the program to become synchronized (p) # Put all process objects in a list, managed uniformly using .join. for i in lst: () print("Send the second e-mail: I want to say, raise my salary to 60,000 a month.") """ ### 2 Create a process using a custom process class. # (1) Basic syntax import os class MyProcess(Process): def run(self): print("1.subprocessid:{},2.parent processid:{}".format((),())) if __name__ == "__main__": p = MyProcess() () # (2) Custom process classes with parameters class MyProcess(Process): def __init__(self,name): # Manually call the constructor method of the parent class to initialize system members. super().__init__() = name def run(self): print("1.subprocessid:{},2.parent processid:{}".format((),())) print() if __name__ == "__main__": p = MyProcess("I am the parameter.") ()
4. Daemon
### Daemon """ The daemon guards the main process, and is forced to kill the daemon as soon as the main process has finished executing all of its code; """ from multiprocessing import Process import time # (1) Basic syntax """ def func(): # (1) print("start... Current child process") print("end ... Current child process") if __name__ == "__main__": p = Process(target=func) # Set up the daemon before the process is started = True () print("Main process execution ended ... ") """ # (2) Guardian scenarios with multiple child processes. """By default, the master process waits for all non-daemon processes, i.e., child processes, to finish executing before shutting down the program and releasing resources. The daemon is automatically shut down when the main process finishes executing its code. """ """ def func1(): print("start ... func1 Execute the current child process ... ") print("end ... func1 End the current child process ... ") def func2(): count = 1 while True: print("*" * count) (1) count += 1 if __name__ == "__main__": p1 = Process(target=func1) p2 = Process(target=func2) # Make the process p2 a daemon. = True () () print("Main process execution ended ... ") """ # (3) Daemon Purpose: Monitoring Reported Activity def alive(): while True: print("Server 3 sends live message to master monitor: i am ok~") (1) def func(): while True: try: print("Server 3 is responsible for resisting concurrent access for 30,000 user volumes...") (3) # Proactively throw an execution error exception, triggering the except branch. raise RuntimeError except: print("Server 3 can't take it anymore... Come fix me...") break if __name__ == "__main__": p1 = Process(target=alive) p2 = Process(target=func) = True () () # You must wait for the p2 child process to finish executing before releasing the following code from the master process. # The following master process code execution ends, immediately killing the daemon and losing the live reporting function. () print("Main process execution ends .... ") #Homework. """ Using multiprocessing to accomplish concurrency on the tcp server """
Tip:
Trigger the decorator when called The packet will only stick when it's sent out continuously Containers cannot be converted to byte streams,machine interactionjson File objects are iterators,The data returned by the iterator is returned row by row When creating a child process,To allocate resources to it,blocked, will go and execute the following program,This is asynchronous execution,Two main lines The technique of posting text data without refreshing the page is calledajax,It's an asynchronous program Processes are typically asynchronous programs Have a look.super()this function By default, the daemon is killed as soon as the master process finishes.,but will wait for the child process to finish running
summarize
That's all for this post, I hope it helps you and I hope you'll check back for more from me!