multithreading issue with wx.TextCtrl (or underlying GTK+)

只愿长相守 提交于 2019-12-03 20:27:54

The biggest problem is that you can't force the subprocess not to buffer its output, and the standard I/O library of most programs will buffer the output when stdout is a pipe (more accurately, they will go from line-buffering to block buffering). Tools like Expect fix this by running the subprocess in a pseudo-tty, which basically tricks the subprocess into thinking its output is going to a terminal.

There is a Python module called Pexpect, which solves this in the same way that Expect does. I've never used it, so caveat emptor.

pty.spawn() could be useful. Also you can manually create PTYs with pty.openpty() and pass them to popen as stdin/stdout.

If you have access to text-mode program source, you can also disable buffering there.

Cant you just pass the TextCtrl object to your OutputThread and have it directly append text to the output without tying it with the __enter method?

Michael

The calling contract for anything in gtk (upon which wxpython is built and the thing which is failing the assertion) application is that you must only modify the gui controls from the Main Gui thread. So the answer for me is simple:

In C#, I needed to do the following (which is an anonymous lambda function, and there's almost certainly a similar library call in wxpython/gtk)

Gtk.Application.Invoke((_,__) =>
{
    //code which can safely modify the gui goes here
});

And that should sort out your assertion problems... it did for me, at least.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!