Prepare
/Check
— Prepare and Check watchers¶
-
class
Prepare
(loop, callback[, data=None, priority=0])¶ -
class
Check
(loop, callback[, data=None, priority=0])¶ Parameters: Prepare
andCheck
watchers are often (but not always) used in pairs:Prepare
watchers get invoked before the process blocks andCheck
watchers afterwards.You must not call
Loop.start()
(or similar methods that enter the current event loop) orLoop.reset()
from eitherPrepare
orCheck
watchers. Other loops than the current one are fine, however. The rationale behind this is that you do not need to check for recursion in those watchers, i.e. the sequence will always bePrepare
-> blocking ->Check
so if you have one watcher of each kind they will always be called in pairs bracketing the blocking call.They could be used, for example, to track variable changes, implement your own watchers, integrate net-snmp or a coroutine library and lots more. They are also occasionally useful if you cache some data and want to flush it before blocking.
Their main purpose is to integrate other event mechanisms into libev and their use is somewhat advanced. This is done by examining in each
Prepare
callback which file descriptors need to be watched by the other library, registeringIo
watchers for them and starting aTimer
watcher for any timeouts (many libraries provide exactly this functionality). Then, in theCheck
watcher, you check for any events that occurred (by checking the pending status of all watchers and stopping them) and call back into the library. The I/O and timer callbacks will never actually be called (but must be valid nevertheless). When used for this purpose, it is recommended to giveCheck
watchers highest (EV_MAXPRI
) priority, to ensure that they are being run before any other watchers after the poll (this doesn’t matter forPrepare
watchers).Also,
Check
watchers (andPrepare
watchers, too) should not activate (“feed”) events into libev. While libev fully supports this, they might get executed before otherCheck
watchers did their job. AsCheck
watchers are often used to embed other (non-libev) event loops those other event loops might be in an unusable state until theirCheck
watcher ran.