I have a unity (C#) app that is calling a python script by initialising a child process.
When trying to write to the python std::in, I seem to be getting a
write fault on path
error when writing to STD::IN.
When reading from the process's STD::OUT, I get null as a value output.
What's the best way to pipe to the python process for input/output?
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Diagnostics;
public class PythonCaller : MonoBehaviour
{
private string buffer = "";
private Process pythonML;
// Use this for initialization
void Start () {
this.pythonML = new System.Diagnostics.Process();
this.pythonML.StartInfo.FileName = "/bin/bash";
this.pythonML.StartInfo.Arguments =
"{{path to python executable}}/python {{path to python script}}/test.py";
this.pythonML.StartInfo.UseShellExecute = false;
this.pythonML.StartInfo.RedirectStandardInput = true;
this.pythonML.StartInfo.RedirectStandardOutput = true;
this.pythonML.StartInfo.RedirectStandardError = true;
this.pythonML.Start();
this.pythonML.BeginOutputReadLine();
this.pythonML.OutputDataReceived += new DataReceivedEventHandler((sender, e) => {
UnityEngine.Debug.Log(e.Data);
});
this.pythonML.WaitForExit();
}
// Update is called once per frame
void Update () {
foreach (char c in Input.inputString) {
if (c == '\b') {
if (this.buffer.Length > 0) {
this.buffer = this.buffer.Substring(0, this.buffer.Length - 1);
}
} else if (c == '\n' || c == '\r') {
try {
UnityEngine.Debug.Log(this.buffer);
this.pythonML.StandardInput.WriteLine(this.buffer);
}
catch (IOException e) {
UnityEngine.Debug.Log(e.Message);
}
this.buffer = "";
} else {
this.buffer += c;
}
}
}
}
PYTHON CODE:
print('hello world!')