如何使用带有扭曲的 raw_input? [英] How can I use a raw_input with twisted?

查看:38
本文介绍了如何使用带有扭曲的 raw_input?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 raw_input 不能用于扭曲.但是,这是我想要的应用程序.

I am aware that raw_input cannot be used in twisted. However here is my desired application.

我有一个提供交​​互式终端串行端口的硬件.我正在尝试连接到此端口并以异步方式发送命令.我需要这种方式,因为这是一个电机控制器,一旦我发出命令,它就会阻塞"并逃跑(我当前的代码).如果出现问题或危险,我需要能够输入另一个命令,例如 ESTOP.

I have an piece of hardware that provides an interactive terminal serial port. I am trying to connect to this port and send commands in an async manner. I need it this way because this is a motor controller that once I issue a command it will "block" and run away (my current code). I need to be able to enter another command such as ESTOP in case of problems or danger.

我已经阅读了一些关于 twisted.internet.stdio.StandardIO 的内容,但是我运气不佳.对此的任何建议/帮助都会很棒.

I have read some stuff about twisted.internet.stdio.StandardIO however I have not had much luck.. Any advice / help on this would be great.

推荐答案

这里有几个选项可以使用.一种是使用子进程来处理与串行端口的通信,并使用管道来处理父子进程之间的通信(由 Twisted 的进程协议简化).另一种方法是分离出一个单独的 Python 线程并从那里使用 raw_input.正常的线程间通信机制与 Twisted 一起工作得很好.唯一真正的扭曲来自您如何从单独的线程中唤醒扭曲的反应器.主要支持的机制是使用 reactor.callFromThread().在 Twisted 中使用线程需要仔细考虑并且很容易搞砸(这就是为什么它通常不被鼓励)但有时它确实是适合这项工作的工具.

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.

这篇关于如何使用带有扭曲的 raw_input?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆