0

I want to send commands from a script to twinkle (the VOIP software). I thought I could do this with a named pipe so I have created one

mkfifo phonecmd

If I start twinkle and pipe tail from the named pipe to it I can send command by writing to the named pipe

tail -f phonecmd | twinkle -c
...
echo answer > phonecmd

But what I really want is to send commands using a python scripts that monitors the GPIO pins in a raspberry pi, but I cant figure out how to do it.

python gpio.py

will print "answer" or "bye" depending on if a GPIO pin on the raspberry pi is changed.

python gpio.py | phonecmd

This gives me an error as phonecmd is not a command (its a named pipe).

python gpio.py > phonecmd

This only sort of works. The redirect is buffered, so everything is written to the phonecmd pipe only once the python process is closed.

Is there a way to have the redirect or pipe "live"?

  • I think it should be on StackOverflow. But why don't you try to use this fifo as regular file inside of Python script? You can create the FIFO in Python as well. See documentation for os module and mkfifo function – mrc02_kr Oct 22 '18 at 11:02
  • 1
    either use sys.stdout.flush() after writing each command in your python script, or call it with stdbuf -o0 python gpio.py > phonecmd (or stdbuf -oL if the commands are followed by newline). – mosvy Oct 22 '18 at 12:38
  • I haven't used Twinkle for quite a while now, but doesn't it have an API you should use to do this sort of thing? – roaima Oct 22 '18 at 18:58
  • @roaima if it has its undocumented as far as I can tell – c0m4 Oct 23 '18 at 10:32
1

The reason why the pipe operator | doesn't work is because the pipe operator expects another command. The output redirection operator > outputs to a file, and a named pipe is a file in the general sense, just not a regular file.

Output to a pipe is in normally buffered, you can flush the output stream or you can change the buffering of stdout.

In your case the most elegant solution would be to open the named pipe in Python, write the string ("answer" or "bye") and then close the file/pipe. This way you have decoupled the server process and the Python client. You can even restart the server process (twinkle -c) and keep the Python process running.

  • I went with the suggested solution of writing to the file from the python script, witch worked great. Thanks! – c0m4 Oct 23 '18 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.