Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loop task methods for callbacks and coroutines #71

Open
scottwittenburg opened this issue Aug 6, 2021 · 0 comments
Open

Add loop task methods for callbacks and coroutines #71

scottwittenburg opened this issue Aug 6, 2021 · 0 comments
Assignees

Comments

@scottwittenburg
Copy link
Contributor

scottwittenburg commented Aug 6, 2021

When wslink made use of twisted, we had a example that did:

from twisted.internet import task
...
loopTask = task.LoopingCall(callback_method)
loopTask.start(2.0)

and that would schedule callback_method to be called repeatedly every two seconds. We can achieve something similar with just the schedule_callback method in wslink now, but not as simply. It would be nice to provide this capability. A first pass at it might look like:

    def loop_callback(period, callback, *args, **kwargs):
       cancel_handle = None
       canceled = False
       func = functools.partial(callback, *args, **kwargs)
       loop = asyncio.get_running_loop()

       def cancel():
          global canceled

          cancel_handle.cancel()
          canceled = True

       def task():
          global cancel_handle

          if not canceled:
             func()
             cancel_handle = loop.call_later(period, task)

       cancel_handle = loop.call_later(period, task)

       return cancel

Though it would be nice if the return value was an object with a cancel() method, so it behaves like the other scheduling methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant