Python看门狗应用程序

2024-09-26 21:58:52 发布

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

呃。。。我保证我在网上搜索过了,但没有找到满意的答案

我正在尝试为我的python应用程序创建一个“控制器”,我的意思是,它应该被执行,然后生成服务器,等待接收退出信号,如果有必要,重新启动脚本。在

这段代码相当混乱,但这就是我目前所得到的。。。它总是返回无法识别的exitSignal 1,甚至不会生成服务器进程!有人能给我一盏灯吗?在

#Server controller version 0.1
import os
import time
from datetime import datetime

Log = file("C:/Users/Admin/Desktop/Python/Server/WIP.log", 'w')

def runServer():
    exitSignal = os.spawnv(os.P_WAIT, 'C:/Python2.7/python.exe', ['python.exe',         'C:/Users/Admin/Desktop/Python/Server/WIP.py'])
    print str(datetime.today())+" - Server started"
    Log.write("\n"+str(datetime.today())+" - Server started")

    if exitSignal == "0":
        print str(datetime.today())+" - Server exited succesfully."
        Log.write("\n"+str(datetime.today())+" - Server exited succesfully.")

    elif exitSignal == "10":
        print str(datetime.today())+" - Rebooting server immediately."
        Log.write("\n"+str(datetime.today())+" - Rebooting server immediately.")
        runServer()

    elif exitSignal == "11":
        print str(datetime.today())+" - Rebooting server in 5 minutes."
        Log.write("\n"+str(datetime.today())+" - Rebooting server in 5 minutes.")
        time.sleep(300)
        runServer()
        print str(datetime.today())+" - Server rebooted."
        Log.write("\n"+str(datetime.today())+" - Server rebooted.")

    else:
        print str(datetime.today())+" - Unrecognized exitSignal code: %s" % str(exitSignal)
        Log.write("\n"+str(datetime.today())+" - Unrecognized exitSignal code: %s" % str(exitSignal))

if __name__ == "__main__":
    print str(datetime.today())+" - Controller started."
    Log.write("\n"+str(datetime.today())+" - Controller started")
    runServer()

对不起,代码太乱了!(=^,^=)


Tags: import服务器logtodaydatetimeserveroswrite
1条回答
网友
1楼 · 发布于 2024-09-26 21:58:52

作为described here,os.spawnv操作系统是不推荐使用的方法,应使用subprocess模块。在

除此之外,如果总是返回1而没有生成进程,则可能是由于服务器代码的路径错误或服务器部分的错误代码造成的。如果手动启动服务器会发生什么情况?在

相关问题 更多 >

    热门问题