6

I'm adapting this Django management command for my own purposes. The script is a simple while-loop daemon that reads from sys.stdin (line 152, in command.handle()) according to a protocol and writes results to sys.stdout.

I would expect sys.stdin.read() to block until it receives something, but I find that when I run this script, it eats up 100% CPU before any data has been sent or received.

  1. Does sys.stdin.read(n) block?
  2. If not, how can I make this daemon more polite?
  3. Is time.sleep(s) safe to use, or will I miss input or be slow to respond?
David Eyk
  • 12,171
  • 11
  • 63
  • 103

3 Answers3

2

By default, sys.stdin.read() and sys.stdin.read(n) are blocking calls. I would assume the consumption of 100% CPU is actually attributable to streaming data into your script or some other behavior not cited here.

Upon looking at the help documentation for sys.stdin.read, I noticed this:

read(...)

read([size]) -> read at most size bytes, returned as a string.

If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given.

(Emphasis mine.)

This implies blocking mode is the default behavior, which is consistent with my experience. It also led me to track down similar questions on SO. Voila: Non-blocking read on a subprocess.PIPE in python

Good luck with your adaptation!

Community
  • 1
  • 1
MrGomez
  • 23,788
  • 45
  • 72
  • It's actually 100% usage w/o receiving or outputting any data yet. I haven't hooked that part up yet. – David Eyk Dec 05 '10 at 19:21
  • Knowing for sure that it's a blocking call, and hearing the result of @icyrock.com's test, I'm guessing my problem lies elsewhere. – David Eyk Dec 05 '10 at 20:46
  • Yep. The problem was me. I still don't know why I was getting the high CPU usage, but now that I've set up my bridge correctly, everything works fine. – David Eyk Dec 05 '10 at 21:39
1

It works OK on my machine (i.e. blocks with negligent CPU usage while it's reading) - can you check it from a simple command line script before? Also, I tested this within Linux, might be different on other platforms.

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
0

Is there a chance the stream has been closed (e.g. EOF was sent)?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153