Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a raw_input with twisted?

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

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.

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.

like image 447
ril3y Avatar asked Feb 28 '11 14:02

ril3y


1 Answers

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.

like image 116
Rakis Avatar answered Oct 02 '22 20:10

Rakis