laziness, impatience, and hubris | |
PerlMonks |
Re: Dancer2: respond to client while making its own async callby stevieb (Canon) |
on Apr 01, 2021 at 19:33 UTC ( [id://11130686]=note: print w/replies, xml ) | Need Help?? |
I figured it out, using one of my own CPAN distributions no less. I updated my Async::Event::Interval software to accept an "interval" of zero, which instead of looping every N seconds, simply runs a single time. So when the Dancer2 route is called, it'll spin off an external event to fetch and update the data, and then I can return existing data if it exists immediately while the background task is still working. The software also already has a built-in check to see if the event is still running or not. Here's a minimal demo:
Output:
Since the Dancer2 app is always running, I just make the event itself along with the data string (JSON) global, and use IPC::Shareable (which I have CO-MAINT to) to share the data between the processes. The Dancer2 route just calls $event->start if $event->status != -1;, and can then return back to the calling microcontroller immediately while the updater event runs as a separate proc.
In Section
Seekers of Perl Wisdom
|
|