【发布时间】:2020-01-22 22:22:15
【问题描述】:
我正在制作一个与我的世界服务器通信并告诉它启动、停止或任何其他命令的 python 程序。该程序读取一封电子邮件并执行电子邮件中给出的命令。当程序输入启动服务器的命令时,会冻结python程序,直到minecraft服务器停止。
我试图让程序打开一个启动服务器的批处理文件,但是我无法让程序关闭它,或者输入命令,或者读取控制台。
if data[0]+data[1]+data[2]+data[3]+data[4]+data[5] == 'start ':
comm = data.replace('start ','')
try:
mb = int(float(comm)*1024)
#calls the command to start the server
call('java -Xmx' + str(mb) + 'M -Xms' + str(mb) + 'M -jar server.jar nogui')
#program freezes here until server is stopped
except Exception:
call('java -Xmx1024M -Xms1024M -jar server.jar nogui')
print("started server")
elif data == "stop server" or data == "stop":
call('stop')
elif data[0]+data[1]+data[2]+data[3]+data[4] == 'call ':
comm = data.replace('call ','')
call(comm)
print("called command")
elif data[0]+data[1]+data[2] == 'op ':
comm = data
call(comm)
print("op player")
else:
print("not a command")
deletemail(mail,i)
print("deleted item")
我希望看到该程序继续运行并回复电子邮件,但它反而冻结了。服务器停止后程序正常继续关闭。我知道这不是错误,但是有没有办法让 python 程序在服务器运行时继续运行?
【问题讨论】:
-
call方法是什么?那是来自一些标准库吗?是你写的吗? -
我用过os.system
-
调用方法将来自子进程。但是文档说
Run the command described by args. Wait for command to complete, then return the returncode attribute.,所以当你运行启动命令时,进程不会重新启动,直到服务器停止。你需要在后台运行命令 -
最重要的是,将命令作为列表传递,而不是作为构造字符串传递,这是不安全的。