网络:os.fork()创建进程

os.fork()创建进程不能用于Windows

python在Windows上的os模块,没有fork方法,所以不能用os.fork()来创建进程

VxWorks 系统上,也不支持os.fork()

windows下进程创建使用multiprocessing模块。

【windows】
In [49]: import os In [50]: help(os.fork) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-50-d4d4f3427ec1> in <module> ----> 1 help(os.fork) AttributeError: module 'os' has no attribute 'fork'
【linux】
>>> import os >>> help(os.fork) Help on built-in function fork in module posix: fork() Fork a child process. Return 0 to child process and PID of child to parent process. (END)

#-----------------------------------------------------------------------#

1、os.fork()创建进程

  os.fork() 创建进程

  pid = os.fork()

  功能 : 创建进程

  返回值:失败,返回一个负数

      成功 : 将0返回给子进程,将子进程的PID返回给父进程。

import os 
from time import sleep 

print("******************")
a = 1

pid = os.fork()

if pid < 0:
    print("Create process failed")
elif pid == 0:
    print("This is Child process")
    print("a = ",a)
    a = 10000 # 子进程中对全局变量的修改不影响父进程
else:
    sleep(1)
    print("This is parent process")
    print("parent a :",a)
#结果
******************
This is Child process
a =  1
This is parent process
parent a : 1

 

2、获取进程pid

    【1】os.getpid()

        功能:获取进程的PID号

        返回值 : 返回进程的PID号

    【2】os.getppid()

        功能:获取父进程的PID号

        返回值 : 返回父进程的PID号

import os 
from time import sleep

pid = os.fork()

if pid < 0:
    print("create process failed")
elif pid == 0:
    print("Child PID:",os.getpid())
    print("Get parent PID",os.getppid())
else:
    sleep(0.5)
    print("Parent PID:",os.getpid())
    print("Get child PID:",pid)
# 结果
Child PID: 3019
Get parent PID 3018
Parent PID: 3018
Get child PID: 3019

3、进程退出

    【1】os._exit(status)

        功能 : 退出一个进程

        参数 : 进程的退出状态整数

    【2】sys.exit([status])

        功能 : 退出一个进程

        参数 : 默认为0 ,如果传入一个整数则同 _exit()

          传入一个字符串。则在退出时打印该字符串

        sys.exit() 可以通过捕获 SystemExit异常阻止退出

In [51]: import os,sys
    ...:
    ...: # os._exit(0)
    ...: try:
    ...:     sys.exit("退出")
    ...: except SystemExit as e:
    ...:     print("退出原因:",e)
    ...:
    ...: print("Process end")
退出原因: 退出
Process end

 4、os.WEXITSTATUS(status)

    获取原来退出状态

【wait.py】
import
os from time import sleep pid = os.fork() if pid < 0: print("create process faild") elif pid == 0: print("Child process running") sleep(3) print("Child process over") os._exit(3) else: #等子进程执行完毕进行回收 pid,status = os.wait() print(pid,status) print(os.WEXITSTATUS(status)) #原来退出状态 while True: pass

5、pid,status = os.waitpid(pid,option)

    功能 : 在父进程中阻塞等待处理子进程的退出

    参数 : pid, -1 表示等待任意子进程退出

          >0 表示等待对应PID号的子进程退出

         option, 0 表示阻塞等待

            WNOHANG ,表示非阻塞

    返回值:pid,退出的那个子进程的PID号

        status ,子进程的退出状态

    waitpid(-1,0) ===> wait()

【wait-pid.py】
import
os from time import sleep pid = os.fork() if pid < 0: print("create process faild") elif pid == 0: print("Child process running") sleep(3) print("Child process over") os._exit(3) else: while True: sleep(1) pid1,status = os.waitpid(-1,os.WNOHANG) print(pid1,status) if pid1 > 0: break while True: pass

 

posted @ 2021-01-06 12:53  昱成  阅读(195)  评论(0编辑  收藏  举报