Manage background tasks in BaseService
See original GitHub issueWhat is wrong?
BaseService handles background tasks awkwardly right now. For example:
- have to
await asyncio.sleep(0)
in cleanup to make sure that all tasks cancel - often need to catch and drop
OperationCancelled
exceptions
This is what kicked off the thought: https://github.com/ethereum/py-evm/pull/1097/files#diff-3942908a5c806895979f03283db24df3R206
An toy example with these quirks:
class MyService(BaseService):
async def check_connection(self):
while self.is_running:
if not is_connected():
self.wait(reconnect())
try:
await self.sleep(60.0)
except OperationCancelled:
break
async def _run(self):
asyncio.ensure_future(self.check_connection())
async def _cleanup(self):
# We don't need to cancel() anything, but we yield control just so that the coroutines we
# run in the background notice the cancel token has been triggered and return.
# Also, don't use self.sleep() here as the cancel token will be triggered and that will
# raise OperationCancelled.
await asyncio.sleep(0)
How can it be fixed
Add a BaseService.loop_in_background(my_task)
method that handles these things for you. Then services can define something like:
class MyService(BaseService):
async def check_connection(self):
if not is_connected():
self.wait(reconnect())
await self.sleep(60.0)
async def _run(self):
self.loop_in_background(self.check_connection())
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Implement background tasks in microservices with ...
Background tasks and scheduled jobs are something you might need to use in any application, whether or not it follows the microservices ...
Read more >Run and manage periodic background tasks in ASP.NET ...
Run and manage periodic background tasks in ASP.NET Core 6 with C#. Sometimes your web app needs to do work in the background...
Read more >Service
When running low on memory and needing to kill existing processes, ... String) to retrieve a JobScheduler instance for managing occasional background tasks....
Read more >php - How to delegate long background tasks from web ...
Option 1 : Queue using scheduled cron jobs. The challenge with this is your users have no control over when the ...
Read more >Manage login items and background tasks on Mac
Identifying apps using background task management. Administrators should be aware of any items that use helper apps and executables which are ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Second that. How about:
Ah, how about
run_periodic_task
and you can specify the period.