Some of usage Process
Process of join usage
join (): block the main process, the child process after waiting for the end, and then execute down
from multiprocessing import Process
import time
def task():
print('进程 start')
time.sleep(2)
print('进程 end')
if __name__ == '__main__':
p = Process(target=task)
p.start()
p.join()
print('主进程')
Check the angle of the current process pid
- Get pid the current process:
os.getpid()
- Gets the parent process pid of the current process:
os.getppid()
- Get child process pid the current process:
子进程对象.pid
Daemon
Essentially daemon is a child process.
The code of the primary process is finished daemon directly over, but the main process may not yet be over.
from multiprocessing import Process
import time
def task():
print('守护进程 start')
time.sleep(5)
print('守护进程 end')
if __name__ == '__main__':
p = Process(target=task)
p.daemon = True # 把这个子进程定义为守护进程
p.start()
time.sleep(2)
print('主进程')