Io
— I/O watcher¶
-
class
Io
(fd, events, loop, callback[, data=None, priority=0])¶ Parameters: - fd (int or object) – the file descriptor to be monitored, can be an int or any
Python object having a
fileno()
method. - events (int) – either
EV_READ
,EV_WRITE
orEV_READ
|EV_WRITE
. - loop (
Loop
) – loop object responsible for this watcher (accessible throughloop
). - callback (callable) – see
callback
. - data (object) – any Python object you might want to attach to the
watcher (stored in
data
). - priority (int) – see
priority
.
Io
watchers check whether a file descriptor is readable or writable in each iteration of the event loop, or, more precisely, when reading would not block the process and writing would at least be able to write some data. This behaviour is called level-triggering because you keep receiving events as long as the condition persists. Remember you can stop the watcher if you don’t want to act on the event and neither want to receive future events.In general you can register as many read and/or write event watchers per fd as you want. Setting all file descriptors to non-blocking mode is also usually a good idea (but not required).
Another thing you have to watch out for is that it is quite easy to receive “spurious” readiness notifications, that is, your callback might be called with
EV_READ
but a subsequentread()
orrecv()
will actually block because there is no data. It is very easy to get into this situation even with a relatively standard program structure. Thus it is best to always use non-blocking I/O: an extraread()
/recv()
returningEAGAIN
is far preferable to a program hanging until some data arrives.If you cannot run the fd in non-blocking mode, then you have to separately re-test whether a file descriptor is really ready with a known-to-be good interface such as poll(2). Some people additionally use
SIGALRM
and an interval timer, just to be sure you won’t block indefinitely.But really, best use non-blocking mode.
See also
-
set
(fd, events)¶ Parameters: Reconfigures the watcher.
-
fd
¶ Read only
The file descriptor being watched.
-
events
¶ The events being watched.
- fd (int or object) – the file descriptor to be monitored, can be an int or any
Python object having a