Can I read the program from the keyboard and keyboard STDIN?

Can I write a python program that is readable using stdin and keyboard?

What I mean? I want to use it this way:

tail -f LOGFILE | myscript.py

see the log lines appearing on the screen and enter commands from the keyboard?

It sounds like 2 stdin and it bothers me. Is this possible or conceptually wrong?

Thank!

+3
source share
3 answers

A possible solution would be to capture the current tty of users and attach the file stream to the corresponding / dev / tty entry.

This may allow you to capture keyboard input when using stdin as your channel log file.

+4
source

script , bash fifo:

myscript.py <( tail -f LOGFILE )

bash ():

mkfifo /tmp/UNIQUEFILENAME
tail -f LOGFILE > /tmp/UNIQUEFILENAME &
myscript.py /tmp/UNIQUEFILENAME

, stdin.

<() > (), , , "" bash.

+3

, . stdin - .

... | program.py

stdin stdout , . stdin - !

Hacking will combine these two threads into one, but this is not a good way to do this; It does not separate data correctly. If your program really needs to accept keyboard input as well as data transmitted through channels (you are sure that it should look like it is a very controversial thing!), The correct way to do this is to create separate threads to process each input of the streams.

+1
source

Source: https://habr.com/ru/post/1761925/


All Articles