question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Manage background tasks in BaseService

See original GitHub issue

What 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:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cburgdorfcommented, Aug 1, 2018

so I think it’d make sense to have this new helper simply run the coroutine with ensure_future(), and then if necessary we could have a run_periodict_task() on top of that

Second that. How about:

self.run_task(self._sync())
self.run_periodic_task(self.check_connection(), restart_sec=60.0)
1reaction
pipermerriamcommented, Jul 31, 2018

Ah, how about run_periodic_task and you can specify the period.

while True:
    ... # run the task
    self.sleep(period)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found