Python Forum
interactive process for input and output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
interactive process for input and output
#1
Dear All,

I want to create a process with interactive session. Say for example I want to create a new session similar like "python3 -i" - this "python3 -i" takes to different session/prompt where we can provide a command and get the result immediately/real time for each command, until we type/say "exit". Once we say "exit" and then it exits the python3 prompt and it come out.

So this is the interactive session. Like this I wanted to create. Where this process open a new session and we keep entering our command and get the output real time/immediately, once we type exit, then exits the session/prompt and close the process.

I created with subprocess.Open, but it takes the command all at once and all the commands are done and then returns the result/output all together, rather returning real time.

I even tried with asyncio as well, but could not achieved a proper result.

If any idea to achieve that, would be very much helpful.

Please see my below code, but not able to achieve as expected.
import asyncio

async def interact_with_subprocess():
    # Start the subprocess (running 'python3 interactive_script.py')
    process = await asyncio.create_subprocess_exec(
        'python3', '-i',  # External Python script
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
        text=False
    )

    while True:
        # Get user input
        #user_input = input("Enter something (type 'exit' to quit): ")
        user_input = input()

        if user_input.lower() == 'exit':
            print("Exiting subprocess.")
            process.stdin.write('exit\n'.encode('utf-8'))
            await process.stdin.drain()
            break

        # Send the user input to the subprocess
        process.stdin.write(user_input.encode('utf-8') + "\n".encode('utf-8'))
        await process.stdin.drain()
        
        # Read the output asynchronously
        output = await process.stdout.readline()
        print(f"Subprocess Output: {output.strip().decode('utf-8', 'ignore')}")

    # Wait for the subprocess to finish
    await process.wait()
    print("Subprocess finished.")

if __name__ == "__main__":
    asyncio.run(interact_with_subprocess())
What might be the issue here which causes not to achieve the goal.

Regards,
Maiya.
Reply
#2
It seems to me you could achieve this goal by using the tools provided by the code module instead of using python -i. Write a script that simulates a read-eval-print loop and execute this script in a subprocess.

Also I don't understand asyncio, so I would do this with threads.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Same input different output antarling 2 929 Oct-25-2024, 11:28 PM
Last Post: antarling
  output provide the filename along with the input file processed. arjunaram 1 1,519 Apr-13-2023, 08:15 PM
Last Post: menator01
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 2,124 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  serial input/output barryjo 3 5,084 Dec-27-2021, 11:57 PM
Last Post: barryjo
  How to input & output parameters from command line argument shantanu97 1 3,621 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  interactive map and function hurc60248 1 2,482 Jan-08-2021, 09:22 PM
Last Post: Larz60+
  Code giving same output no matter the input. Yort 2 4,735 Dec-20-2020, 05:59 AM
Last Post: buran
  Handling multi-input/output audio in python bor1904 4 4,904 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  single input infinite output problem Chase91 2 2,709 Sep-23-2020, 10:01 PM
Last Post: Chase91
  My code is giving my an output of zero, no matter what value I input PiyushBanarjee 1 2,510 Jul-01-2020, 04:34 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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