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()
抱歉,代码混乱! (=^,^=)
Erm... i promise i searched thoroughly the web for this and couldn't find a satisfatory answer so >.<
I'm trying to make a 'controller' for my python app, i mean, it should be executed and then spawn the server, wait to pickup exit signals and if necessary, restart the script.
This code is pretty messy but that's what i've come with so far... It always returns with unrecognized exitSignal 1 and won't even spawn the server process! Can someone give me a light please?
#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()
Sorry for the messy code! (=^,^=)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 此处所述, os.spawnv 是一个已弃用的方法,您应该使用子进程 模块。
除此之外,如果总是返回 1 并且没有生成进程,可能是由于服务器代码的路径错误或服务器部分的代码错误。如果手动启动服务器会发生什么?
As described here, os.spawnv is a deprecated method, you should use the subprocess module.
Apart this, if you always got 1 returned and no process spawned, maybe it is due to a bad path to the server code or a bad code for the server part. What happen if you launch the server manually ?