can not read correctly from STDIN
Question: I have a weird problem to read from STDIN in a python script.

Here is my use case. I have rsyslog configured with an output module so
rsyslog can pipe log messages to my Python script.

My Python script is really trivial :

    
    
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    import sys
    
    fd = open('/tmp/testrsyslogomoutput.txt', 'a')
    fd.write("Receiving log message : \n%s\n" % ('-'.join(sys.stdin.readlines())))
    fd.close()
    

If I run `echo "foo" | mypythonscript.py` I can get "foo" in the target file `/tmp/testrsyslogomoutput.txt`. However when I run it within rsyslog, messages seems to be sent only when I stop/restart rsyslog (I believe some buffer is flushed at some point).

I first thought it was a problem with Rsyslog. So I replaced my python program
with a shell one, without changing anything to the rsyslog configuration. The
shell script works perfectly with rsyslog and as you can see in the code
below, the script is really trivial:

    
    
    #! /bin/sh
    cat /dev/stdin >> /tmp/testrsyslogomoutput.txt
    

Since my shell script works but my Python one does not, I believe I made a
mistake somewhere in my Python code but I can not find where. If you could
point me to my mistake(s) that would be great.

Thanks in advance :)


Answer: `readlines` will not return until it has finished reading the file. Since the
pipe feeding stdin never finishes, `readlines` never finishes either. Stopping
rsyslog closes the pipe and lets it finish.

