在linux 系统中 使用os.fork 新建一个子进程,11秒后杀掉该子进程
新建的子进程不受父进程的影响,即使父进程结束了,子进程仍然继续
记得要在子进程中写退出命令 exit() , 否则会走父进程结束。
-
import os
-
import time
-
import signal
-
def child_process():
-
a = 0
-
for a in range(0,100):
-
time.sleep(1)
-
print ("I am the child process and my PID is: %d" % os.getpid())
-
-
print ("the child is exiting.")
-
exit()
-
-
def parent_process():
-
-
print ("I am the parent process whit PID : %d" % os.getpid())
-
-
childId = os.fork()
-
-
if childId == 0:
-
#在子进程中
-
child_process()
-
else:
-
#在父进程中
-
print ("inside the parent process")
-
print ("my child's pid is : %d" % childId)
-
-
print("exit parent function")
-
return childId
-
print('-------')
-
-
def kill_pr(childId):
-
os.kill(childId,signal.SIGKILL)
执行结果是:
-
[root@webServer zjx]# python test.py
I am the parent process whit PID : 5799
inside the parent process
my child's pid is : 5800
exit parent function
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
I am the child process and my PID is: 5800
-
[root@webServer zjx]#
阅读(1427) | 评论(0) | 转发(0) |