子POPEN标准输出锁定了?(subprocess popen stdout locking up?

2019-09-16 12:44发布

嗨使用subprocess.Popen时有一些问题,标准输出阅读

daniel@desktop:~$ python -V
Python 2.7.3

继承人的代码:(注释代码是有些事情我已经试过)

import subprocess

RUN = './hlds_run -game cstrike -maxplayers 11'

p = subprocess.Popen(RUN.split(), shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

while 1:
    try:
        out = p.stdout.readline()
        #if out == '':
        #   out = p.stdout.read()
        #p.stdout.flush()
    except: 
        p.terminate()
        break

    try:
        err = p.stderr.readline()
        #if err == '':
        #   err = p.stderr.read()
        #p.stderr.flush()
    except:
        p.terminate()
        break
    if out != '':
        print out.rstrip()

    if err != '':
        print err.rstrip()


    #print '\n' #constantly prints new lines until "Calling BreakpadMiniDumpSystemInit."

这是输出IM连接到服务器和断开时得到:

daniel@desktop:~/hlds$ python hlds.py 
Auto detecting CPU
Using breakpad crash handler
Using Pentium II Optimised binary.
Setting breakpad minidump AppID = 10
Auto-restarting the server on crash
Forcing breakpad minidump interfaces to load

Looking up breakpad interfaces from steamclient
Console initialized.
Calling BreakpadMiniDumpSystemInit
scandir failed:/home/daniel/hlds/./valve/SAVE
Installing breakpad exception handler for appid(10)/version(5447)
scandir failed:/home/daniel/hlds/./platform/SAVE
Looking up breakpad interfaces from steamclient
Protocol version 48
Calling BreakpadMiniDumpSystemInit

while循环后,锁定了:

Calling BreakpadMiniDumpSystemInit.

但服务器仍在运行,我可以连接,运行命令等..


如果我运行:

 ./hlds_run -game cstrike -maxplayers 11 >> stdout.log 2>&1

我得到stdout.log以下的输出:

daniel@desktop:~/hlds$ cat stdout.log 
Auto detecting CPU
Using Pentium II Optimised binary.
Auto-restarting the server on crash

Console initialized.
Using breakpad crash handler
Setting breakpad minidump AppID = 10
Forcing breakpad minidump interfaces to load
Looking up breakpad interfaces from steamclient
Calling BreakpadMiniDumpSystemInit
Installing breakpad exception handler for appid(10)/version(5447)
scandir failed:/home/daniel/hlds/./valve/SAVE
scandir failed:/home/daniel/hlds/./platform/SAVE
Protocol version 48
Exe version 1.1.2.6/Stdio (cstrike)
Exe build: 14:06:24 Sep 23 2011 (5447)
STEAM Auth Server
Server IP address 127.0.1.1:27015
couldn't exec listip.cfg
couldn't exec banned.cfg
Looking up breakpad interfaces from steamclient
Calling BreakpadMiniDumpSystemInit
scandir failed:/home/daniel/hlds/./valve/SAVE
scandir failed:/home/daniel/hlds/./platform/SAVE

Could not establish connection to Steam servers.
Reconnected to Steam servers.
   VAC secure mode is activated.
ERROR: couldn't open custom.hpk.
JAMES HETFIELD : hello!
Dropped JAMES HETFIELD from server
Reason:  Client sent 'drop'
Sat Apr 14 00:10:54 CEST 2012: Server Quit

但是如果我不这样做2>&1我仍然得到在标准输出这个输出和stdout.log休息:

daniel@desktop:~/hlds$ ./hlds_run -game cstrike -maxplayers 11 >> stdout.log
Using breakpad crash handler
Setting breakpad minidump AppID = 10
Forcing breakpad minidump interfaces to load
Looking up breakpad interfaces from steamclient
Calling BreakpadMiniDumpSystemInit
Installing breakpad exception handler for appid(10)/version(5447)
Looking up breakpad interfaces from steamclient
Calling BreakpadMiniDumpSystemInit

试图创造一个ServerManager的调度员和作为子学习经验:]

所有的帮助表示赞赏! :)

Answer 1:

什么情况是,该管的人会充满了数据,从而阻断子,而你的Python程序被阻止试图读取从另一个管的管线,从而导致死锁。 你可能想使用某种轮询(选择/投票/ epoll的)管子上的,而不是做阻塞读取。

一个快速的黑客是做非阻塞在你的while循环读取,但将使用大量的CPU会导致你的Python程序。

看看对于文档选择上解决问题的非哈克的方式更多信息模块。



Answer 2:

你已经能够解决这个使用选择? 我发现,当我开始SRCDS甚至非阻塞它不会走过去的“呼叫BreakpadMiniDumpSystemInit”:

p = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
fcntl.fcntl(p.stdout, fcntl.F_SETFL, fcntl.fcntl(p.stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
print p.stdout.read()

这将打印到“BreakpadMiniDumpSystemInit”的消息,还呼吁阅读()抛出“资源暂时不可用”,直到东西写入p.stdin。



文章来源: subprocess popen stdout locking up?