[Python-ideas] Tulip / PEP 3156 - subprocess events

Guido van Rossum guido at python.org
Fri Jan 18 22:52:59 CET 2013


On Fri, Jan 18, 2013 at 1:37 PM, Oleg Broytman <phd at phdru.name> wrote:
> On Sat, Jan 19, 2013 at 01:25:31AM +0400, Oleg Broytman <phd at phdru.name> wrote:
>> On Fri, Jan 18, 2013 at 09:01:32PM +0000, Paul Moore <p.f.moore at gmail.com> wrote:
>> > Hmm, I'm looking at a pipe transport on Unix, and I find I don't know
>> > enough about programming Unix. How do I set a file descriptor
>> > (specifically a pipe) in Unix to be nonblocking? For a socket,
>> > sock.setblocking(False) does the job. But for a pipe/file, the only
>> > thing I can see is the O_NONBLOCK flag to os.open/os.pipe2. Is it not
>> > possible to set an already open file descriptor to be nonblocking?
>>
>>     F_GETFL
>>         Read the file descriptor's flags.
>>     F_SETFL
>>         Set the file status flags part of the descriptor's flags to the
>>         value specified by arg. Remaining bits (access mode, file creation
>>         flags) in arg are ignored. On Linux this command can only change the
>>         O_APPEND, O_NONBLOCK, O_ASYNC, and O_DIRECT flags.
>
>    So you have to call fnctl() on the pipe's descriptor to F_GETFL
> flags, set O_NONBLOCK and call fnctl() to F_SETFL the new flags back.

Here's my code for this:

def _setnonblocking(fd):
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list