我如何在 Python3.3 中使用 fork() [英] how can i use fork() in Python3.3

查看:35
本文介绍了我如何在 Python3.3 中使用 fork()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python3.3 中使用 fork()**这是我的代码:

how can i use fork() in Python3.3 **This is My code :

输入:

#!/usr/bin/env python
import os

def Child_process():
    print("We are in Child_Process")
    print("My PID: %d"%os.getpid())
    print("Child_Process is exiting")

def Parent_process():
    print("-------Parent_process---------")
    wpid = os.fork()
    if wpid==0:
        print("wpid is 0 means We are in Child_process")
        print("Child :%d"%wpid)
        Child_process()
    else:
        print("Execute Parent_process")
        print("Parent_process %d"%wpid)
        Parent_process()

Parent_process()

输出:

C:\Python33\python.exe C:/Users/Iem-Prog/Desktop/Py/Fork

Traceback (most recent call last):

  File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 21, in <module>
-------Parent_process---------
    Parent_process()
  File "C:/Users/Iem-Prog/Desktop/Py/Fork", line 11, in Parent_process
    wpid = os.fork()

AttributeError: 'module' object has no attribute 'fork'

推荐答案

os.fork 仅在类 Unix 系统中可用.你不能在 Windows 中使用它.

os.fork is only available in Unix-like system. You cannot use that in Windows.

os.fork()

fork 一个子进程.在子进程中返回 0 和子进程 ID在父级.如果发生错误,则引发 OSError.

Fork a child process. Return 0 in the child and the child’s process id in the parent. If an error occurs OSError is raised.

注意一些平台,包括 FreeBSD <= 6.3、Cygwin 和 OS/2 EMX在线程中使用 fork() 时存在已知问题.

Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have known issues when using fork() from a thread.

可用性:Unix.

这篇关于我如何在 Python3.3 中使用 fork()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆