Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
STDIN not working
#1
Hi all.

Anyone would have an idea why the STDIN is not working here? I start a server application for a game as a subprocess, and the STDOUT part works and can read all the output, but the process.stdin.write doesn't do anything. (If I add process.communicate() after that line, it will work once and no more. I understand tho that 'communicate' shouldn't be used here since I want to keep sending commands, just mentioning it.)

def READ_CONSOL():
    while True:
        LINE = process.stdout.readline().rstrip()
        print((LINE.decode().strip()))

process = subprocess.Popen([r"C:\Games\Far Cry 2 Fortune's Edition\bin\FC2ServerLauncher.exe", "-noredirectstdin"], stdout = subprocess.PIPE, stdin = subprocess.PIPE)

start_new_thread(READ_CONSOL, ())

while True:
    COMMAND = input()
    process.stdin.write((COMMAND + '\n').encode())
(The "-noredirectstdin" part is a command line parameter the app needs to be launched with for STDIN to work, according to it's readme.)

Another question I would have is how I could make the subprocess close/terminate automatically when I close the main window.

Thanks in advance!
Reply
#2
Try adding a flush after the write.

while True:
    COMMAND = input()
    process.stdin.write((COMMAND + '\n').encode())
    process.stdin.flush()
Reply
#3
(Sep-04-2021, 01:22 PM)H84Gabor Wrote: Another question I would have is how I could make the subprocess close/terminate automatically when I close the main window.

Could try putting your code in a try/finally block and kill the subprocess in the finally block.


import time


def event_loop():
    count = 0
    while True:
        count += 1
        print(f"Counting: {count}")
        time.sleep(1)


try:
    event_loop()
finally:
    print("I'm done!")
    # kill the subprocess from here
Reply
#4
Thank you!

The 'process.stdin.flush()' worked great! About the second thing I think I was a bit confused. What I wanted to do was to make that 'FC2ServerLauncher.exe' application terminate somehow when the consol the messages are printed to is closed. But I think to do that I would need to make that window also a subprocess so the pyhton script can detect it was closed and terminate the application and itself.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is stdin not connected to a tty, but can still be used interactively? stevendaprano 1 1,005 Sep-24-2022, 05:06 PM
Last Post: Gribouillis
  How to read a file using stdin? pyth0nus3r 1 2,828 Aug-24-2019, 01:14 AM
Last Post: Larz60+
  What is the sys.stdin.isatty() command? pyth0nus3r 2 11,410 Aug-22-2019, 04:37 PM
Last Post: pyth0nus3r
  getting error "exec_proc.stdin.write(b'yes\n') IOError: [Errno 32] Broken pipe" Birju_Barot 0 2,939 Jul-23-2019, 11:50 AM
Last Post: Birju_Barot
  is it safe to close stdin Skaperen 1 2,659 Apr-04-2019, 06:57 AM
Last Post: Gribouillis
  Need help with reading input from stdin into array list Annie123 2 5,058 Mar-24-2019, 01:19 PM
Last Post: Annie123
  detect if sys.stdin is ... Skaperen 4 8,085 May-30-2018, 11:49 PM
Last Post: Skaperen
  storing lines from stdin in a list bghosh 2 2,978 May-02-2018, 01:12 PM
Last Post: gruntfutuk
  How can I use GNU readline when the value of sys.stdin has been changed? thePhysicist8 6 7,079 Dec-30-2016, 10:09 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020