只是想知道是否有人可以帮助我.我遇到的问题是我os.fork()获取几个信息并将它们发送到文件,但检查fork进程是否工作.
import sys
import time
import os
import re
ADDRESS = argv[1]
sendBytes = argv[2]
proID2 = os.fork()
if proID2 == 0:
os.system('ping -c 20 ' + ADDRESS + ' > testStuff2.txt')
os._exit(0)
print proID2
finn = True
while finn == True:
time.sleep(1)
finn = os.path.exists("/proc/" + str(proID2))
print os.path.exists("/proc/" + str(proID2))
print 'eeup out of it ' + str(proID2)
Run Code Online (Sandbox Code Playgroud)
我认为os.path.exists()可能不是正确的使用方法.
谢谢.
虽然您可以使用os.fork()和os.wait()(参见下面的示例),但您最好使用子进程模块中的方法.
import os, sys
child_pid = os.fork()
if child_pid == 0:
# child process
os.system('ping -c 20 www.google.com >/tmp/ping.out')
sys.exit(0)
print "In the parent, child pid is %d" % child_pid
#pid, status = os.wait()
pid, status = os.waitpid(child_pid, 0)
print "wait returned, pid = %d, status = %d" % (pid, status)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18158 次 |
| 最近记录: |