DaemonInterface.run is no longer just coroutine, but it is now AsyncGenerator.
It yields either a message or None, so the application knows if it should forward the output of the generator to the router or not.
Message handling methods use try-except instead of hasattr.
Pros
De-coupling MessageRouter from all daemons.
Now DaemonInterface doesn't have to have MessageRouter as a member to send a message.
Better syntax to write a daemonic-process.
Instead of calling message_router.send_message_async, it can simply yield messages along the process.
Easier to control the interval of daemon process all at once.
Application can influence how often the daemon has the event loop.
More clear when/how to call asyncio.sleep in the daemon.
For example, since re-calling of the async-generator is done by the Application, daemons no longer need to worry about re-trying and simply add an if statement that calls await asyncio.sleep.
So helpers such as async_retry or data_pipe_monitor is no longer needed.
Cons?
Daemons must call the asyncio.sleep or yield something in the run generator.
Otherwise it will occupy the event loop for quite sometime...
It was always the case even without this change,
but you didn't have to think about it with data_pipe_monitor helper since it is doing it itself.
You also have to think about the timing of when asyncio.sleep is called in the generator.
AsyncGenerator type-annotation is a bit tedious.
You need to make a generator to write a test. (One more step for testing.)
Memo
I also tried making handlers AsyncGenerator to try using asend.
I wanted to try this to avoid checking attribute to see if it is the very first message to process.
But it was not so neat after all since you always have to asendNone,
and it seems like making things a bit slower.....
So I tried using try-exception instead of checking attributes to save time in the run-time.
Related to #127
Main Changes
DaemonInterface.run
is no longer just coroutine, but it is nowAsyncGenerator
. It yields either a message orNone
, so the application knows if it should forward the output of the generator to the router or not.try
-except
instead ofhasattr
.Pros
MessageRouter
from all daemons. NowDaemonInterface
doesn't have to haveMessageRouter
as a member to send a message.message_router.send_message_async
, it can simplyyield
messages along the process.Application
can influence how often the daemon has the event loop.asyncio.sleep
in the daemon. For example, since re-calling of the async-generator is done by theApplication
, daemons no longer need to worry about re-trying and simply add an if statement that callsawait asyncio.sleep
. So helpers such asasync_retry
ordata_pipe_monitor
is no longer needed.Cons?
asyncio.sleep
oryield
something in therun
generator. Otherwise it will occupy the event loop for quite sometime... It was always the case even without this change, but you didn't have to think about it withdata_pipe_monitor
helper since it is doing it itself. You also have to think about the timing of whenasyncio.sleep
is called in the generator.Memo
I also tried making handlers
AsyncGenerator
to try usingasend
. I wanted to try this to avoid checking attribute to see if it is the very first message to process. But it was not so neat after all since you always have toasend
None
, and it seems like making things a bit slower..... So I tried using try-exception instead of checking attributes to save time in the run-time.