Let’s first recall the problem we explained last time
Review
1. Two ways to create a process
Method 1:
from multiprocessing import Process
def task(name):
print('%s is running' %name)
if __name__ == '__main__':
obj=Process(taget=task,args=('egon',))
obj.start() # send a signal to the operating system
print('main')
task('xxx')
Method 2:
from multiprocessing import Process
class MyTask(Process):
def run(self):
print('%s is running' %self.name)
if __name__ == '__main__':
obj=MyTask()
obj.start() # Send a signal to the operating system
2. Address space isolation between
processes 3. Other methods of process objects
obj.join(1)
obj.terminate()
obj.is_alive()
obj.pid
obj.name
4. Zombie process and orphan process
Today's content:
Daemon process (understanding) is based on the process of turning the child process into a daemon process. As long as the parent process ends, the daemon process will also end. Please see the following code demonstration
# !/usr/bin/env python # -*- coding:utf-8 -*- # author:Albert from multiprocessing import Process import time def task(name): print('%s is running' % name) time.sleep(2) if __name__ == '__main__': obj = Process(target=task,args=( ' egon ' ,)) # obj.daemon = True # This is the daemon code, with this line, there will be child processes, without this line, only the main process obj.start () print ( ' this is the main process ' )
Mutual exclusion lock
IPC mechanism (queue, pipeline)
producer consumer model (key)
process pool (key)