Is it possible to output and control streams other than stdin, stdout & stderr? (Python)

This is a python question, but also a linux / BSD question.

I have a python script with two streams, one downloading data from the Internet and the other sending data to the device via the serial port. Both of these threads print a lot of status information in stdout using the python module logging.

What I would like is to have two terminal windows open side by side, and each terminal window displays output from one stream, and not messages from both interlaces in one window.

Are there file descriptors other than stdin, stdout and stderr for recording and connecting to other terminal windows? Perhaps this desire is better fulfilled using the graphical interface?

I am not sure how to start with this.

edit: I tried writing status messages to two different files instead of printing them to stdout and then controlling these two files using tail -fin other terminal windows, but this does not work for direct monitoring because the files are not written until until you name close()them.

+4
source share
2 answers

First, set up your logging formatter to include the Theme> field ( https://docs.python.org/2/library/logging.html#logrecord-attributes ). Then change the record destination to some file instead of stdout.

# A simple logger as print
import logging
import logging.handlers

hdr = logging.FileHandler(filename='output.log')
hdr.setFormatter(logging.Formatter('[%(asctime)s] thread=%(thread)s:%(levelname)s: %(message)s'))
logger = logging.getLogger(__name__)
logger.addHandler(hdr)
logger.setLevel(logging.DEBUG)

import threading


def func():
    logger.info('test message')


for i in range(2):
    threading.Thread(target=func).start()

:

% tail -f output.log
[2015-09-28 15:14:49,782] thread=4344852480:INFO: test message
[2015-09-28 15:14:49,782] thread=4349059072:INFO: test message

script, tail -f output.log | grep thread=<THREAD_ID> .

+2

, , , flush() .

: , , logging.StreamHandler , , , logging.FileHandler. , .

0

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


All Articles