What's the inverse of this?

fcntl.fcntl(MyStream.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

I've tried

fcntl.fcntl(MyStream.fileno(), fcntl.F_SETFL, os.O_BLOCK)

but it results in a AttributeError: 'module' object has no attribute 'O_BLOCK'.

To give a little background, I'm piping data in chunks into STDIN of a popen'ed process. If there is data waiting in STDOUT, I need to read it straight away and pass it back to the client (hence switching to non-blocking). However, when I've finished piping data in, I then need to block until I get an EOF on STDOUT.

If there's a better way to do this (ideally cross-platform?) then I'm all ears

有帮助吗?

解决方案

Get the current flag and turn off O_NONBLOCK bit:

flag = fcntl.fcntl(MyStream.fileno(), fcntl.F_GETFL)
fcntl.fcntl(MyStream.fileno(), fcntl.F_SETFL, flag & ~os.O_NONBLOCK)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top