- 获取主进程和子进程编号os.getpid()
- 在子进程内获取他的父进程编号os.getppid()
- 通过进程编号停止进程os.kill(process_id, 9)
import multiprocessing
import time
import os
def dance():
dance_process_id = os.getpid()
print("dance进程编号: ", dance_process_id, multiprocessing.current_process)
dance_process_parent_id = os.getppid()
print("dance父进程编号: ", dance_process_parent_id)
for item in range(1, 5):
print("跳舞...")
time.sleep(0.2)
# 9 表示强行停止该进程
os.kill(dance_process_id, 9)
pass
pass
def sing():
sing_process_id = os.getpid()
print("sing进程编号: ", sing_process_id, multiprocessing.current_process)
for item in range(1, 5):
print("唱歌...")
time.sleep(0.2)
pass
pass
if __name__ == "__main__":
dance_process = multiprocessing.Process(target=dance)
sing_process = multiprocessing.Process(target=sing)
dance_process.start()
sing_process.start()
main_processs_id = os.getpid()
print("主进程编号: ", main_processs_id, multiprocessing.current_process)