OBV
OBV

Reputation: 1259

How to determine if an fd can be read with os.read(fd,[buffer[) without hanging?

Before doing: os.read(fd,1024) I would like to check that there will be output rather than it hanging until output it received. Since fd is an int object, I can't do:

os.fstat(f.fileno()).st_size

If I could get the size, I could check it is not 0.

Sorry if this is really simple, I am new to python.

Upvotes: 1

Views: 314

Answers (1)

falsetru
falsetru

Reputation: 369324

Use select.select. (In windows, you can only it with socket):

import select

...

r, _, _ = select.select([fd], [], [], 0)
if r:
    data = os.read(fd, 1024)

Upvotes: 2

Related Questions