检查子进程是否已启动

2024-10-03 11:13:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序可以启动另外四个程序,我想让它们同时运行,我可以用subprocess.Popen()来做,我想检查它们是否正确启动。如果没有,什么也不做,等到下次有人打电话。我正在windows上工作,这是我目前所拥有的,但我不确定它是否能按我认为的方式工作,我不知道如何测试它,除了坐在那里,希望它失败,但继续。有人能告诉我这是否有效,如果不行为什么?你知道吗

import subprocess
import time
from datetime import datetime

while 1:

    Day = time.strftime('%d')
    Month = time.strftime('%m')
    Year = time.strftime('%Y')  #if conditions are just right(the day changes before month and year are calculated) then the dates could get off (highly unlikely)
    start = time.clock()
    try:
        p1 = subprocess.Popen(['python', 'C:/Users/tnabrelsfo/Documents/Programs/strippers/TransmitterStrip.py'], stdout=None)
    except OSError:
        print('Error with Transmitter')
    try:
        p2 = subprocess.Popen(['python', 'C:/Users/tnabrelsfo/Documents/Programs/strippers/ReceiverStrip.py'], stdout=None)
    except OSError:
        print('Error with Receiver')
    try:
        p3 = subprocess.Popen(['python', 'C:/Users/tnabrelsfo/Documents/Programs/strippers/UIDStrip.py'], stdout=None)
    except OSError:
        print('Error with UID')
    try:
        p4 = subprocess.Popen(['python', 'C:/Users/tnabrelsfo/Documents/Programs/strippers/BlueStripper.py'], stdout=None)
    except OSError:
        print('Error with Blue')


    p1.wait()
    p2.wait()
    p3.wait()
    p4.wait()
    print('Duration: %.2f' % (time.clock()-start))
    print('\n' + str(datetime.now()) + '\n')
    print('Done with run')

    for x in range(301):
            time.sleep(1)

Tags: pynonetimewithstdoutusersdocumentssubprocess