2016-07-04 20 views
0

subprocess.Popen()のプロセスにコマンドを渡す必要がありますが、それを実行すると、後でstdin.close()を使用した場合にのみ機能します。私のコードは以下の通りです。サブプロセスにコマンドを送信する.Popen()プロセス

sprocess.stdin.write('/stop'.encode()) 
sprocess.stdin.flush() 
sprocess.stdin.close() 
sprocess.stdout.close() 

作品が、それは理由のstdin.close()であると私はパイプを閉じずにこれを行うことができるようにする必要があります。

パイプを閉じずにプロセスにコマンドを渡すにはどうしたらいいですか?

#!/usr/bin/python3 

import discord 
import asyncio 
from subprocess import Popen, PIPE 

client = discord.Client() 

@client.event 
async def on_ready(): 
    print('Bot ID: '+ client.user.id +'\nREADY\n') 

@client.event 
async def on_message(message): 
    if message.content.startswith('/start'): 
     if message.channel.name == "server-console": 
      await client.send_message(message.channel, '**SERVER STARTING**') 
      print('SERVER STARTING') 
      global sprocess 
      global soutput 
      sprocess = Popen('java -Xmx2048M -Xms2048M -jar minecraft_server.jar', 
             shell=True, 
             stdout=PIPE, 
             stdin=PIPE) 

    elif message.content.startswith('/stop'): 
     if message.channel.name == "server-console": 
      print('SERVER STOPPING') 
      sprocess.stdin.write('/stop'.encode()) 
      sprocess.stdin.flush() 
      sprocess.stdin.close() 
      sprocess.stdout.close() 

    elif message.content.startswith('/restart'): 
     if message.channel.name == "server-console": 
      print('SERVER RESTARTING') 

    elif message.content.startswith('/'): 
     if message.channel.name == "server-console": 
      sprocess.stdin.write(message.clean_content.encode()) 
      sprocess.stdin.flush() 
      print(message.clean_content) 
      print('COMMAND SENT') 

client.run('token') 
+0

'sprocess.stdin.write( '/ stop'.encode())'は改行を追加しないことに注意してください。あなたのサブプロセスはおそらく改行またはEOFのいずれかで示される入力の* line *を期待しています。 'stdin'を閉じると、そのEOFが送られます。代わりに 'sprocess.stdin.write( '/ stop \ n'.encode())'を送信してみてください。 – MisterMiyagi

+0

@MisterMiyagiあなたはそれを私がそれを受け入れることができるという答えにしてください。 – spikespaz

答えて

1

sprocess.stdin.write('/stop'.encode())は改行を追加しません。あなたのサブプロセスはおそらく行のの入力を期待しています。改行またはEOFのいずれかで表されます。 stdinを閉じると、そのEOFが送信されます。

代わりにsprocess.stdin.write('/stop\n'.encode())を送信してみます。

関連する問題