Mar-27-2025, 04:06 AM
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.
Regards,
Maiya.
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.
