1

Suppose I run a .exe program within python whatever os or subprocess, the .exe program is designed to pop up some different results with different arguments, my steps are the following:

  1. In python run .exe first(keep it alive, it will have communication with hardware and do initialization)
  2. send different arguments to this .exe and collect the different outputs.

I tried the following code: hello.py import sys

for arg in sys.argv:
    print(arg)
print("Hello World!")

test.py

  import subprocess
  command='./exe_example/output/hello/hello.exe a b'.split()
  result = subprocess.run(command, stdout=subprocess.PIPE, text=True)
  print(result.stdout)

the output is:

a b Hello World!

but how to change the input argv and get the result without running the whole .exe command again?

UPDATES: I changed the hello.py as follows:

import sys
while True:
    a = input()
    print('response = ',a)

after compiling to .exe, I could manually run it in the dos window

hello.exe
a
response =  a
b
response =  b
c
response =  c

but I still don't know how to run it in python

  • `argv` inputs are read from the command line once when the program starts. You can't send them while the app is running, because they weren't in the command line when the app was started. If you need to test different arguments, you need to run the app multiple times from the start. – Ken White Nov 07 '22 at 03:47
  • thanks, any other way to solve this problem? in this situation, the .exe program should look like a com Port, so I could keep sending different commands to the Port, and receive the different results – adameye2020 Nov 07 '22 at 03:51
  • Then you'll have to build a communications layer into it, and write a separate app that sends commands via that layer. – Ken White Nov 07 '22 at 03:53
  • thanks, could you send an example link? I totally do not know how to do it – adameye2020 Nov 07 '22 at 03:56
  • I don't have one. You're as capable of searching for one as I would be, and I don't need it. :-) – Ken White Nov 07 '22 at 03:58

1 Answers1

0

finally, I figured out, first from this post, I added flush()

cmd = "E:/exe_example/TestCl.exe"
p = Popen(cmd, stdin=PIPE,stdout=PIPE, bufsize=0)
p.stdin.write('getall\n'.encode()) 
p.stdin.flush()
for i in range(48):
    print(p.stdout.readline())

then, very important, if I use read(), because the .exe is always listening to the input, so it will hang up forever and never output, in this case, readline() is very important