我正在编写 python 脚本来启动服务器,可能在后台或在不同的进程中,然后在终止启动的服务器之前进一步进行一些处理。
一旦其余的处理结束,就终止启动的服务器。
例如
server_cmd = 'launch_server.exe -source '+ inputfile
print server_cmd
cmd_pid = subprocess.Popen(server_cmd).pid
...
...
... #Continue doing some processing
cmd_pid.terminate() # Once the processing is done, terminate the server
启动服务器后脚本无法继续运行,因为服务器可能正在无限循环中运行以监听请求。有没有一种好方法可以在后台发送此进程,以便它不需要命令行输入。
我使用的是Python 2.7.8
最佳答案
奇怪的是,您的脚本在启动服务器命令后没有继续。在 subprocess
模块中,Popen
启动另一个子进程,而父进程(您的脚本)应该继续运行。
但是,您的代码中已经存在一个错误:cmd_pid
是一个 int
对象,并且没有 terminate
方法。您应该使用 subprocess.Popen
对象来调用 terminate
方法。
关于python - 用于运行服务器并继续的 Windows python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28450678/