Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to output to and monitor 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 threads, one downloading data from the web and the other sending data to a device over a serial port. Both of these threads print a lot of status information to stdout using python's logging module.

What I would like is to have two terminal windows open, side by side, and have each terminal window show the output from one thread, rather than have the messages from both interleaved in a single window.

Are there file descriptors other than stdin, stdout & stderr to write to and connect to other terminal windows? Perhaps this wish is better fulfilled with a GUI?

I'm not sure how to get started with this.

edit: I've tried writing status messages to two different files instead of printing them to stdout, and then monitoring these two files with tail -f in other terminal windows, but this doesn't work for live monitoring because the files aren't written to until you call close() on them.

like image 829
beibei2 Avatar asked Sep 28 '15 06:09

beibei2


People also ask

What is the difference between stdin stdout and stderr?

stdin − It stands for standard input, and is used for taking text as an input. stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error.

What are the three standard streams?

There are 3 type of standard streams; standard input (stdin), standard output (stdout) and standard error (stderror).

What is stderr stdin stdout?

The stdin , stdout , and stderr global constant pointers are standard streams for input, output, and error output. By default, standard input is read from the keyboard, while standard output and standard error are printed to the screen. The following stream pointers are available to access the standard streams: Pointer.


1 Answers

First, customize your logging formatter to include a Thread id field (https://docs.python.org/2/library/logging.html#logrecord-attributes). Then change your logging 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()

Your logging output may look like this now:

% 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

Run your script, open two individual terminals, and use the command tail -f output.log | grep thread=<THREAD_ID> to monitor logs by thread ID.

like image 72
piglei Avatar answered Sep 22 '22 00:09

piglei