Does python's sys.stdin.read() block?

前端 未结 3 1421
粉色の甜心
粉色の甜心 2021-01-12 17:18

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()

相关标签:
3条回答
  • 2021-01-12 17:31

    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.

    0 讨论(0)
  • 2021-01-12 17:31

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

    0 讨论(0)
  • 2021-01-12 17:47

    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!

    0 讨论(0)
提交回复
热议问题