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 the manpage from python, i use the following code but it totally overloads my cpu.
#!/usr/bin/python -u
import sys
while 1:
for line in sys.stdin.readlines():
do something useful
Is there any good way to solve the high cpu usage?
Edit:
All your solutions don't work. I give you exactly my problem.
You can configure the apache2 daemon that he sends every logline to a program and not to write in a logfile.
This looks something like that:
CustomLog "|/usr/bin/python -u /usr/local/bin/client.py" combined
Apache2 expects from my script that it runs always, waits for data on sys.stdin and parses it then there is data.
If i only use a for loop the script will exit, because at a point there is no data in sys.stdin and apache2 will say ohh your script exited unexpectedly.
If i use a while true loop my script will use 100% cpu usage.
readline()andreadlines().readlines()will read all the input from stdin until EOF (basically callingread()then splitting by newline). That means that it will return for the first time when stdin is closed. Future calls toreadlines()(orread()andreadline()) on stdin will return[](or""for read/readline). Suggested reading: docs.python.org/2/tutorial/inputoutput.html unix.stackexchange.com/questions/103885/… - FluxLemur