При попытке использовать stdin и stdout в C # (Unity) для конвейерной передачи в процесс Python, я получаю около десятка или около того транзакций, и процесс прерывается и появляется ошибка «ObjectDisposedException: объект использовался после удаления».
Попробовав несколько более очевидных вещей, я привожу здесь проблему, возможно, кто-то знает только правильную технику.Заранее спасибо.
Вот код запуска C #:
Process pyProcess; // <=== fixed
ProcessStartInfo pyStartInfo;
public StreamReader pyStreamReader;
public StreamWriter pyStreamWriter;
public void startPython()
{
// Create new process start info
pyStartInfo = new ProcessStartInfo(pyPath)
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
Arguments = pyApp + " " + pyArgs
};
pyProcess = new Process { StartInfo = pyStartInfo };
pyProcess.Start();
pyStreamReader = pyProcess.StandardOutput;
pyStreamWriter = pyProcess.StandardInput;
pyStreamWriter.WriteLine("Hello!");
string str = pyStreamReader.ReadLine();
Debug.LogFormat(str + "\n");
}
void Start()
{
if(testPython == true)
startPython();
Вот фрагмент, который генерирует данные, отправляемые Python при каждом обновлении ...
if (controller.testPython)
{
string str, python;
str = String.Format("data to send");
pyStreamWriter.DiscardBufferedData(); #<==== fixed
pyStreamWriter.WriteLine(str);
python = pyStreamReader.ReadLine();
Debug.LogFormat("python says: " + python + "\n");
}
А вотупрощенный процесс Python, который повторяет данные
while True:
cmd = input() # read a command from c#
print(cmd) # process the cmd, here we just echo it back to c#