A History of computer development
Multi-channel technology
1. Spatial multiplexing on: a plurality of programs share a common set of computer hardware
2. multiplexed on time
Save state switching +
1. When a program encounters an operating system IO operation will deprive the CPU to execute permissions of the program (to improve CPU utilization)
2. When a program prolonged occupation of CPU, operating system will be deprived of its execute permissions (reduces the performance of programs)
Theory II. Process
Program: a bunch of code processes: running programs
1. synchronous asynchronous: that is submitted with tasks
Sync: perform the task after task of submitting place to wait and get results without doing anything during the return walk (surface program is stuck)
Asynchronous: After the task is not in place waiting for submission, but continues with the next line of code (the result is to be, but it is used to obtain otherwise
Running state programs represented: 2. Non-blocking blocking
Blocking: blocking state, waiting
Non-blocking: ready state, running state
He stressed: synchronous asynchronous non-blocking is blocking two pairs of concepts can not be confused
III. Create a process in two ways
Create a process that is in memory to re-open piece of memory space, the generated code will allow throw into
A corresponding process in memory is a separate memory space
Data between processes and the process is not isolated but can be implemented directly interact indirectly through some interactive technology
The first:
. 1 Import multiprocessing Import Process 2 Import Time . 3 DEF Test (): . 4 Print ( ' child processes running IS! ' ) . 5 the time.sleep (2 ) . 6 Print ( ' sub-process over IS! ' ) . 7 . 8 IF the __name__ == ' __main__ ' : . 9 P = process (target = Test) 10 p.start () . 11 Print ( ' parent ')
The second:
from multiprocessing import Process import time class MyProcess(Process): def __init__(self,name): super.__init__() self.name = name def run(self): print("%s is running" %self.name) time.sleep(3) print('%s is over' %self.name) if __name__ == '__main__': PMyProcess = ( ' child process ' ) p.start () Print ( " main process " )
IV. Methods join process
1 from multiprocessing import Process 2 import time 3 4 def test(name,i): 5 print('%s is running'%name) 6 time.sleep(i) 7 print('%s is over'%name) 8 9 if __name__ == '__main__': 10 p_list= [] 11 for i in range(3): 12 Process = P (target = Test, args = ( ' Process S% ' % I, I)) 13 is p.start () 14 p_list.append (P) 15 for P in P_LIST: 16 p.join () # host process Code wait for the child to continue to run until the end of the run 17 # main process code wait for the child run ended 18 Print ( ' primary process ' )
V. inter-process data is isolated
A corresponding process in memory is a separate memory space
Data between processes and the process is not isolated from direct interaction
. 1 from multiprocessing Import Process 2 Import Time . 3 . 4 Money = 100 . 5 . 6 DEF Test (): . 7 Global Money . 8 Money = 9999 . 9 10 IF the __name__ == ' __main__ ' : . 11 P = Process (target = Test) 12 is p.start () 13 is p.join () 14 Print (Money) # results or data isolation between the process and the process 100 are respective independent memory space
VI. Process object and other methods
. 1 from multiprocessing Import Process, current_process 2 Import OS . 3 Import Time . 4 . 5 DEF Test (): . 6 Print ( ' child process ' ) . 7 Print (current_process () PID.) . 8 Print (os.getpid ()) # both is a query meaning the current process id 9 Print (os.getppid ()) # get the parent process id 10 the time.sleep (3 ) 11 Print ( ' the child over iS ' ) 12 13 IF __name__ == ' __main__ ' : 14 the p-Process = (target = the Test) 15 p.start () 16 p.terminate () # kill the current process is actually tells the operating system to help you kill a process 17 Time. SLEEP (0.1 ) 18 Print (p.is_alive ()) # determine whether the process alive result False 19 Print (os.getpid ()) 20 Print (os.getppid ()) # parent is the main process of the implementation of its program process id such as parcharm
VII. Zombie process and orphaned
1. zombie process
The end of the process is a zombie process, all processes will step into zombie
Although zombie process is over, but he has an id number, etc. need to be recovered this work needs to recover its parent to do
The parent process child process recycling resources in two ways:
1.join method
2. The parent process Wrongful Death
2. orphaned
The child did not die, and the parent accidental death
For linux there will be children's welfare (init) to adopt the accidental death of the parent process child process he created
VIII. Daemon
. 1 from multiprocessing Import Process 2 Import Time . 3 . 4 DEF Test (name) . 5 Print ( ' % S manifold normally alive ' % name) . 6 the time.sleep (. 3 ) . 7 Print ( ' % S manifold natural death ' % name) . 8 . 9 IF the __name__ == ' __main__ ' : 10 P = Process (target = Test, args = ( ' Egon ' ,)) . 11 = True p.daemon # The process is set to daemon, these words must be placed in front of the start, otherwise it will error 12 the time.sleep (0.1 ) 13 Print ( ' emperor died ' )
IX. Mutex
With rush tickets here as example, if more than one person at the same time grab a ticket, then certainly to look at network speed and hand speed. So if multiple processes to operate to grab votes, how will they be able to distinguish the order yet. This uses the mutex.
When multiple processes operating the same data can cause confusion data
This time must lock into a concurrent serial processing
While reducing the efficiency but to improve the security of data
note:
1. Do not use the lock easily lead to deadlocks
2. The only part of the process of data lock, do not lock in the global
Lock must produce to the child process to use in the main process
1 from multiprocessing import Process,Lock 2 import time 3 imort json 4 5 #查票 6 def search(i): 7 with open ('data','r',encoding = 'utf-8') as f: 8 data = f.read() 9 t_d = json.loads(data) 10 print('用户%s查询余票为%s' %(i,t_d.get('ticket'))) 11 12 #买票 13 def buy(i): 14 with open ('data','r',encoding = 'utf-8') as f: 15 data = f.read() 16 t_d = json.loads(data) 17 time.sleep(1) 18 if t_d.get('ticket')>0: 19 t_d['ticket'] -= 1 20 with open('Data ' , ' W ' , encoding = ' UTF-. 8 ' ) AS F: 21 is The json.dump (T_d, F) 22 is Print ( ' User grab votes success% s ' % I) 23 is the else : 24 Print ( ' no votes a ' ) 25 26 27 DEF RUN (i, mutex): 28 Search (i) 29 mutex.acquire () # grab the lock if someone else grabbed the lock must wait for the person to release the lock 30 Buy (i) 31 mutex.release () #Release lock 32 33 is 34 is IF the __name__ == ' __main__ ' : 35 the mutex = Lock () # generates a lock 36 for I in Range (10 ): 37 [ P = Process (target = RUN, args = (I, the mutex) ) 38 is p.start ()
operation data
{“ticket”:1}