Executor
depeche_db.Executor(db_dsn, stimulation_interval=0.5, disable_signals=False)
Executor is a class that runs handlers on notifications.
Typical usage:
executor = Executor(db_dsn="postgresql://localhost:5432/mydb")
executor.register(MyRunOnNotification())
executor.run() # this will stop on SIGTERM or SIGINT
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db_dsn |
str
|
DSN for the PostgreSQL database |
required |
stimulation_interval |
float
|
Seconds; Every interval, all the handlers will run once. Zero & negative values disable stimulation. |
0.5
|
disable_signals |
bool
|
Disable catching SIGINT/SIGTERM, useful if you want to run the Executor in a sub-thread |
False
|
register(handler)
Registers a handler to be run on notifications.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
RunOnNotification
|
Handler to register |
required |
run()
Runs the executor.
depeche_db.RunOnNotification
Bases: Protocol
Run on notification is a protocol that allows objects to be run when a notification is received on a channel. Objects that implement this protocol can be registered with a Executor object.
Implemented by
notification_channel: str
property
Returns notification channel name.
interested_in_notification(notification: dict) -> bool
Signals whether the object is interested in a notification.
If this method returns False
, the object's run
method will not be called
because of this notification.
run(budget: TimeBudget) -> Optional[RunOnNotificationResult]
Runs the object. This method needs to return when a chunk of work has been done. It needs to return within reasonable time after the given time budget is over.
Returns:
Type | Description |
---|---|
Optional[RunOnNotificationResult]
|
WORK_REMAINING if there is still work to be done |
Optional[RunOnNotificationResult]
|
DONE_FOR_NOW if there is no work to be done |
Optional[RunOnNotificationResult]
|
None will be interpreted as DONE_FOR_NOW (backwards compatibility) |
stop()
If the object's run
method has a loop, this method can be used to
exit the loop earlier.
Will be called in a separate thread.
take_notification_hint(notification: dict)
This will be called when a notification is received. Make sure that
the implementation of this method is fast, as it will be called for every
notification received on the channel.
Also make sure that this method is thread-safe, as it can be called
from different threads. Especially, it should be thread-safe against
the run
method.
depeche_db.TimeBudget
Bases: Protocol
over_budget()
Returns True
if the budget is over.