How can I use a raw_input with twisted?

允我心安 提交于 2019-12-05 05:57:52
Rakis

You have a couple of options here that you can use. One would be to use a child process to handle communicating with the serial port and a pipe to communicate between the parent and child (which is simplified by Twisted's Process Protocol). Another is to spin off a separate Python thread and use raw_input from there. Normal inter-thread communication mechanisms work fine with Twisted. The only real twist comes from how you wake the twisted reactor from the separate thread. The primary supported mechanism for this is using reactor.callFromThread(). Use of threads in Twisted requires some careful thought and is easy to screw up (which is why it's generally discouraged) but occasionally it really is the right tool for the job.

Have you seen the StandardIO examples? There are a couple in the Twisted core examples, stdin.py and stdiodemo.py. There is a more advanced example that involves line editing and history in Twisted Conch. You can run this one with python -m twisted.conch.stdio to see it in action. The Conch example probably only works on POSIX platforms, not Windows.

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