Python wait until data is in sys.stdin

后端 未结 9 1300
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 23:00

my problem is the following:

My pythons script receives data via sys.stdin, but it needs to wait until new data is available on sys.stdin.

As described in th

9条回答
  •  盖世英雄少女心
    2020-12-15 23:09

    This works for me, code of /tmp/alog.py:

    #! /usr/bin/python
    
    import sys
    
    fout = open("/tmp/alog.log", "a")
    
    while True:
        dat = sys.stdin.readline()
        fout.write(dat)
        fout.flush()
    

    in http.conf:

    CustomLog "|/tmp/alog.py" combined
    

    The key is don't use

    for dat in sys.stdin:
    

    You will wait there get nothing. And for testing, remember fout.flush(), otherwise you may not see output. I test on fedora 15, python 2.7.1, Apache 2.2, not cpu load, alog.py will exists in memory, if you ps you can see it.

提交回复
热议问题