gpt4 book ai didi

python - 我如何在 Python3.3 中使用 fork()

转载 作者:可可西里 更新时间:2023-11-01 10:53:41 34 4
gpt4 key购买 nike

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

输入:

#!/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()

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.

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

Availability: Unix.

关于python - 我如何在 Python3.3 中使用 fork(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19547443/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com