this post was submitted on 09 Dec 2023
145 points (87.2% liked)

Programmer Humor

32000 readers
936 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 22 points 9 months ago (7 children)

Context:

I have 2 Terminals open, in one is a Python Terminal Chat client running(pt3). I want to sent text from the other terminal(pt2) to the chat. It does write the text to pt3 but pt3 dosent 'see' the text.

If i send over "hallo world" it prints on the terminal but dosent show up in chat.

github of the python termina terminal chat

[–] [email protected] 15 points 9 months ago

My first thought was, can you flush the buffer?

[–] [email protected] 11 points 9 months ago (1 children)

A PTS is a single character device. Writing to it causes output to appear on the terminal buffer, reading from it reads from the input buffer. So, writing to it like you do from a separate shell effectively does the same as calling print() from python which has it as inherited stdio. There is a way to write to a PTS input buffer but it's not straightforward and works in a completely different way. Use something like tmux instead, or better, sockets.

[–] [email protected] 4 points 9 months ago

thank you!

tmux did thr tick for me

[–] [email protected] 8 points 9 months ago

Just guessing here but could it be because you haven't set up correctly pt2 as stdin for pt3, try to invoke the command as

script.py 
[–] [email protected] 4 points 9 months ago

This is because the "tty" (by which I mean the device named by the output of ´tty´) is only displaying what is sent to it. Be it from the keyboard or pty2.

The fact that the keyboard also fills an input buffer from python has to do with how python and the keyboard are attached to the same input file device which is a separate thing from them having same output file device.

If anything that could output to tty2 could inject inputs to something using tty2 as an input buffer, that would be a security nightmare.

Now, I'll sit back and let Cunningham's law kick in.

[–] [email protected] 3 points 9 months ago

Named pipes are your friends. Or sockets.

[–] [email protected] 3 points 9 months ago* (last edited 9 months ago)

With the correct permission you should be able to write straight into pythons stdin from /proc like

Cat whatever > /proc/$pythonpid/fd/0

[–] aspirate2959 2 points 9 months ago

https://unix.stackexchange.com/questions/281319/tool-to-watch-other-ttys-for-linux

I've not played with it, but I used bsd "watch" and this is advertised as similar.