半百了,继续加油!!!
Process
Process 11. Create process(windows) 2
2. Orphan(孤儿) process(understand) 2
3. Zombie(僵尸) process(understand) 2
4. Three states of a process 3
5. Mutilprocessing module 3
5.1. Method one(simple) 4
5.2. Method two(simple) 4
5.3. Parent-child process isolation via(通过) 'global' validation(验证) 4
5.4. join() 5
5.4.1. simple 5
5.4.2. complex 6
5.5. name() and pid() 7
5.6. terminate() and is_alive()(understand) 7
5.7. os.getpid() and os.getppid() 8
1.Create process(windows)
fork process(linux)
Windows through CreateProcess create a new process to load(装入) the code to run and copy the data from the parent process. The windows copy is a little different from the parent process, but the linux copy is the same as the parent process.
2.Orphan(孤儿) process(understand)
If a parent process exits and one or more of its child processes are still running, those child processes become orphaned processes.The orphaned process will be adopted by the init process (process number 0) and the init process will complete the state collection(状态收集) on them.
3.Zombie(僵尸) process(understand)
Child use the 'fork'(a tool) to create a process, if the exit of the child(the child process exits not entirely(完全地), taking up the pid number and leaving other information) and the parent is not calling(调用) 'wait' or 'waitpid' get child process status information, so the child process status information is still stored in the system.This process is called a zombie process。
If the parent process is not dead and the zombie program is not processed(处理), it will take up operating system pid and memory space,it will be harmful.
4.Three states of a process
operation - ready - block
1,The process is blocked waiting for input
2,The operating system selects another program
3,The operating system selects this program
4,There is valid(有效) input
5.Mutilprocessing module
Create a child process object that copies the data of the parent process as a module(把父进程当成模块) to implement multiple process.
Because the child process copies all the data of the parent process, we must use '__main__' to prevent recursive, if not use '__main__', the child process copies the parent process, and it will get the object of the child process again and copy again.
5.1.Method one(simple)
from multiprocessing import Process
import time
def task(name):
print("%s start" % name)
time.sleep(2)
print("%s end" % name)
if __name__ == '__main__':
obj = Process(target=task, args=(("child process",)))
obj.start()
print("parent process")
5.2.Method two(simple)
from multiprocessing import Process
import time
class MyProcess(Process):
def run(self):
print("%s start" % self.name)
time.sleep(2)
print("%s end" % self.name)
if __name__ == '__main__':
obj = MyProcess()
obj.start()
print("parent process")
5.3.Parent-child process isolation via(通过) 'global' validation(验证)
from multiprocessing import Process
x = 100
def task():
global x
x = 0
print("child process %s" % x) # x = 0
if __name__ == '__main__':
obj = Process(target=task)
obj.start()
print("parent process %s" % x) # x = 100
5.4.join()
5.4.1.simple
from multiprocessing import Process
import time
import random
class Piao(Process):
def __init__(self,name):
self.name=name
super().__init__()
def run(self):
print('%s is piaoing' %self.name)
time.sleep(random.randrange(1,3))
print('%s is piao end' %self.name)
if '__name__' == '__main__':
obj=Piao('egon')
obj.start()
obj.join() # Wait for the child process to finish running before running the next line
print('start')
5.4.2.complex
from multiprocessing import Process
import time
import random
def piao(name):
print('%s is piaoing' %name)
time.sleep(random.randint(1,3))
print('%s is piao end' %name)
if '__name__' == '__main__':
p1=Process(target=piao,args=('egon',))
p2=Process(target=piao,args=('alex',))
p3=Process(target=piao,args=('yuanhao',))
p4=Process(target=piao,args=('wupeiqi',))
p1.start()
p2.start()
p3.start()
p4.start()
p1.join()
p2.join()
p3.join()
p4.join()
The above 'start' process and 'join' process can be abbreviated as:
p_l=[p1,p2,p3,p4]
for p in p_l:
p.start()
for p in p_l:
p.join()
The detailed analysis is as follows:
# process starts as soon as start, so when p1-p4.start(), there are already four concurrent processes in the system
# while we p1. Join () is waiting for p1 to end, it is true that p1 will stay stuck as long as the main thread does not end, which is also the key to the problem
# the join is to let the main thread, etc., and the p1 - p4 is still the concurrent execution, p1. Join, the rest of the p2, p3, p4 is still running, such as # p1. Join the end, may the p2, p3, p4 have already ended, so p2. Join, p3. Join. P4. Join directly through testing, without waiting for
conclusion: so the total time spent on the four joins is still the time spent running the longest running process
5.5.name() and pid()
from multiprocessing import Process
import time
import random
class Piao(Process):
def __init__(self,name):
# self.name=name
# super().__init__() #The '__init__' method of process executes 'self.name= piao-1', so when added here, it overrides our 'self.name=name'
#The correct way to name the process we started
super().__init__()
self.name=name
def run(self):
print('%s is piaoing' %self.name)
time.sleep(random.randrange(1,3))
print('%s is piao end' %self.name)
p=Piao('egon')
p.start()
print('start')
print(p.pid) # View the subprocess pid
5.6.terminate() and is_alive()(understand)
from multiprocessing import Process
import time
import random
class Piao(Process):
def __init__(self,name):
self.name=name
super().__init__()
def run(self):
print('%s is piaoing' %self.name)
time.sleep(random.randrange(1,5))
print('%s is piao end' %self.name)
p1=Piao('egon1')
p1.start()
p1.terminate()#Close the process, not immediately, so the results of 'is_alive' immediate results(立刻查看的结果) may still be alive
print(p1.is_alive()) #Result is True
print('start')
print(p1.is_alive()) #Result is False
5.7.os.getpid() and os.getppid()
from multiprocessing import Process
import time
import os
def task():
print('child process id:%s parent process id:%s ' %(os.getpid(),os.getppid()))
time.sleep(200)
if __name__ == '__main__':
p1=Process(target=task)
p1.start()
print('parent',os.getpid(),os.getppid())
谢谢观看!!!!