2013-10-23 87 views
0
使用fork()

我怎樣才能在Python3.3 使用fork()**這是我的代碼:我怎樣才能在Python3.3

輸入:

#!/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' 
+0

您使用哪種操作系統?例如,這在windows下是不可能的。 – User

+0

調查'subprocess'和'multiprocessing'模塊。 – zwol

+2

這是一件好事,它沒有奏效,因爲它無限自稱,變成叉子炸彈。 – interjay

回答

7

os.fork僅適用於類Unix系統。你不能在Windows中使用它。

os.fork()

叉一個子進程。在子代中返回0,子代的進程編號爲 。如果發生OSError錯誤。

請注意,某些平臺包括FreeBSD < = 6.3,Cygwin和OS/2 EMX 在使用線程中的fork()時存在已知問題。

可用性:Unix。

2

由於os.fork不可用你的目標,而不是考慮使用subprocess module甚至(電池 - 不包括在內)envoy

這些創建一個方便抽象兒童周圍的抽象。