當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python os.waitid()用法及代碼示例


Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。

os.waitid()進程使用Python中的方法來等待一個或多個子進程的完成。

用法: os.waitid(idtype, id, options)

參數:
id:一個整數值,表示要等待的子進程的ID。
idtype:idtype和id參數指定方法等待的子級。

返回類型:此方法返回一個對象,該對象表示siginfo_t結構中包含的數據。

代碼1:用於os.waitid()方法

# Python program to explain os.waitid() method  
  
# importing os module   
import os  
  
# Create a child process 
# using os.fork() method  
pid = os.fork() 
  
  
# a Non-zero process id (pid) 
# indicates the parent process  
if pid : 
      
    # Wait for the completion of 
    # child process using 
    # os.waitid() method 
  
    # Specify idtype 
    idtype = os.P_PID 
  
    # Specify id 
    id = pid 
      
    # Specify option 
    option = os.WEXITED 
  
    status = os.waitid(idtype, id, option) 
  
    print("\nIn parent process-") 
      
    # Print status 
    print("Status of child process:") 
    print(status) 
  
  
else : 
    print("In Child process-") 
    print("Process ID:", os.getpid()) 
    print("Hello ! Geeks") 
    print("Exiting..") 
     
輸出:
In Child process-
Process ID: 10309
Hello! Geeks
Exiting..

In parent process-
Status of child process:
posix.waitid_result(si_pid=10309, si_uid=1000, si_signo=17, si_status=0, si_code=1)

代碼2:用於os.waitid()方法

# Python program to explain os.waitid() method  
  
# importing os module   
import os  
  
# Create a child process 
# using os.fork() method  
pid = os.fork() 
  
  
# a Non-zero process id (pid) 
# indicates the parent process  
if pid : 
      
    # Create one more child process  
    pid2 = os.fork() 
      
    if pid2 : 
      
        # Wait for the completion of 
        # any child processes using 
        # os.waitid() method 
  
        # Specify idtype 
        idtype = os.P_ALL 
  
        # Specify id 
        # As idtype is os.P_ALL 
                # method will wait for  
        # any children and specified id  
        # is ignored.              
        id = pid 
  
        # Specify option 
        option = os.WSTOPPED | os.WEXITED 
      
        status = os.waitid(idtype, id, option) 
  
        print("\nIn parent process-") 
          
        # Print status 
        print("Status of completed child process:") 
        print(status) 
  
    else : 
        print("\nIn Second Child process-") 
        print("Process ID:", os.getpid()) 
        print("Hey ! There  ") 
        print("Exiting") 
  
  
else : 
    print("In First Child process-") 
    print("Process ID:", os.getpid()) 
    print("Hello ! Geeks") 
    print("Exiting") 
     
輸出:
In First Child process-
Process ID: 11524
Hello! Geeks
Exiting

In Second Child process-
Process ID: 11525
Hey! There  
Exiting

In parent process-
Status of completed child process:
posix.waitid_result(si_pid=11524, si_uid=1000, si_signo=17, si_status=0, si_code=1)

參考文獻:



相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.waitid() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。