こんにちは、subprocess.Popen を使用しているときに stdout の読み取りに問題があります。
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."
これは、サーバーに接続して切断したときに得られる出力です。
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 に表示され、残りは 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
サブプロセス学習体験としてサーバーマネージャーとディスパッチャーを作成しようとしています:]
すべての助けに感謝します!:)