So I think I found a way to solve this as it appears that the subprocess is continuous it was quite straightforward.
Here is the code I used:
import subprocess as sb
from time import sleep
#initiate the process with the batch file
proc=sb.Popen(bat_file, shell=False, stdin=sb.PIPE, stdout=sb.PIPE, stderr=sb.PIPE,)
sleep(18)#Make sure python gets initiated
for command in commands:#send commands
#print all the console outputs
I used stdin.write since it was the only way to send multiple commands instead of having to restart my python process for each instance, and I have also made a mode that concatenate everything together in order to all be handled by
As an example the input of the function could be :
commands=['import numpy as np','a=np.rand(3,2,1)','print(a)']
EDIT_ important things to consider
For anyone planning to rely on this, if you plan to send string there are 2 important things to consider!
- You must find a way to preserve your quotation marks on each end of the string, here are the possible ways to preserve them
- Another thing that is important to consider is if you plan on using string that indicate paths or contain
\ in them, add
'r' to the start of the string so the encode will interpret it as a raw string and not throw an error cause by
\ and the characters surrounding it.