0

I need to send informations to a bot launched by a client. I've tried to do something like

bot_process = subprocess.Popen(argv, \
                   stdin=subprocess.PIPE, \
                   stdout=subprocess.PIPE)

while True:
    bot_stdout = bot_process.communicate(message.encode())[0]
    print(bot_stdout)

But function communicate() stop the process after one call.

I need to send continually informations to my bot and listen his response. Do you have any solutions to run bot before loop and write in bot stdin + listen to bot stdout inside the loop ?

Thanks !

  • You'll need to read from `bot_process.stdout` and write to `bot_process.stdin`, not use the "one-shot" `.communicate()` function. – AKX Jan 13 '22 at 11:37
  • Thank you ! ```print (bot_process.stdout.readline())``` seems to freeze my client. How can you read stdout ? – mapapin Jan 13 '22 at 11:46
  • If you need to be able to do other things while waiting for outputs, you'll either need a separate thread, or `selectors`. – AKX Jan 13 '22 at 12:01
  • No, i need to wait for outputs, but ```print (bot_process.stdout.readline())```never read anything i suppose, because everything is freezed. My bot is a while True: input + print – mapapin Jan 13 '22 at 14:35
  • If the other process waits for input before printing anything, then naturally you'll need to send it something first. – AKX Jan 13 '22 at 15:03
  • Yep like this ``` while True: bot_process.sdint.write(b'test') print(bot_process.stdout.readline()) ``` this doesn't work for me with a bot like : ``` While True: test = input() print(test) ``` – mapapin Jan 13 '22 at 16:21
  • You're not writing a newline – `input()` reads until one. As far as your bot is concerned, you're just leaving it hanging. Try `.write(b'test\n')`. :) – AKX Jan 13 '22 at 16:42
  • I'm sorry but it's not working, is it ok on your side ? – mapapin Jan 13 '22 at 16:46
  • You will also need to make sure the stdin stream is not buffered, or call `.flush()`, e.g. `bot_process.stdin.flush()`. – AKX Jan 13 '22 at 20:38
  • YES IT WORKS; THANKS – mapapin Jan 13 '22 at 22:16

0 Answers0