Python subprocess.popen错误的pid [英] Python subprocess.popen wrong pid

查看:626
本文介绍了Python subprocess.popen错误的pid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个用Python监视游戏服务器的程序. 为此,需要检查在屏幕会话中启动的游戏服务器进程是否仍在运行,为此,我需要它的pid,但是我没有得到屏幕会话的pid,我获得一个仅在popen会话处于活动状态之前才存在的pid.屏幕会话的pid比pid popen的.pid()方法返回的pid高一个.我知道这可能是由于shell = True引起的,但是我正在使用shell = False运行它.有什么干净的方法可以在屏幕进程中获取屏幕进程/游戏服务器的pid?我唯一的其他解决方案是只编写.pid()+ 1,但是我想那将是一个可怕的解决方法.

I'm trying to write a program that monitors gameservers in Python. For that, n need to look up whether the process of the gameserver, which is started in a screen session is still running, and for that, i need it's pid, however, i don't get the pid of the screen session, i get a pid that only exists till the popen session is active. The pid of the screen session is one pid higher than the pid popen's .pid() method returns. I know that this could be caused through shell=True, but i'm running it with shell=False. Any clean way to get the pid of the screen process/gameserver in the screen process? My only other solution would be just to write .pid()+1, but i guess that would be a horrible workaround.

这是我的代码的相关部分:

Here is the relevant part of my code:

proc = Popen(["screen", "-dmS", self.name, "./srcds_run", "-port", str(self.port), "-game", self.modDir], shell=False)

提前谢谢.

推荐答案

您的问题不是python,而是屏幕.

Your problem is not with python, it is with screen.

当使用-dmS启动屏幕并进入后台时,它会自行执行一些派生和执行操作,因此,如果使用strace来检查系统调用,则会产生一个新的PID,您将看到如何在屏幕二进制文件中执行屏幕二进制文件您可以看到返回的PID,稍后将执行其他过程并完成操作.

When you launch screen with -dmS and it goes into the background it does some forking and execveing by itself, therefore, a new PID, if you use strace to inspect the systemcalls, you will see how the screen binary gets executed in the returned PID that you can see, it later executes other processes and finishes.

这是您在ps通话中看到的另一个过程

It is the other process you are seeing in your ps calls

这篇关于Python subprocess.popen错误的pid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆