【问题标题】:Windows python script to run a server and continueWindows python脚本运行服务器并继续
【发布时间】:2015-04-11 14:34:26
【问题描述】:

我正在编写我的 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

【问题讨论】:

  • 能否包含您的完整代码。

标签: python windows subprocess popen


【解决方案1】:

奇怪的是,您的脚本在启动服务器命令后没有继续。在subprocess 模块中,Popen 启动另一个子进程,而父进程(您的脚本)应该继续前进。

但是在您的代码中已经存在一个错误:cmd_pid 是一个 int 对象并且没有 terminate 方法。您应该使用subprocess.Popen 对象来调用terminate 方法。

【讨论】:

    【解决方案2】:

    稍作改动即可解决问题

    server_proc = subprocess.Popen(server_cmd, stdout=subprocess.PIPE)

    server_proc.terminate()

    感谢徐在终止时的更正。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 2017-12-16
      • 2011-09-08
      • 2016-06-26
      • 1970-01-01
      • 2011-08-26
      相关资源
      最近更新 更多