在尝试使用C#(Unity)中的stdin和stdout来通过管道传输到Python进程时,我得到了大约12个事务,进程中断,并出现错误“ObjectDisposedException:the object was used after being disposed”
在尝试了一些更明显的事情之后,我把问题带到这里也许有人知道正确的技术。提前谢谢。你知道吗
以下是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#
经过一点试验,我发现
之前
解决了主要问题,这种简单的管道形式似乎可以工作,至少对于我观察到的数百个事务来说是这样。你知道吗
我还必须在范围外声明pyProcess,这样代码就不会释放它的句柄。解决了ObjectDisposed异常。你知道吗
相关问题 更多 >
编程相关推荐