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.

`AsyncIOExecutor` and class-based job callbacks

See original GitHub issue

Is your feature request related to a problem? Please describe. I have a class along the lines

class MyTask:
    ...

    (async) def __call__(self, *args, **kwargs):
        ....

and use this as job callback via scheduler.add_job(MyTask(), **job_kwargs). This works fine on 3.x with BackgroundScheduler. However, when using AsyncIOScheduler, when the job runs, asyncio will issue a warning about MyTask.__call__ not having been awaited.

This is due to util.iscoroutinefunction returning False for MyTask() in AsyncIOExecutor._do_submit_job:

https://github.com/agronholm/apscheduler/blob/4ded0dceb05acc95b8ef14cfe397e91391a5d39a/apscheduler/executors/asyncio.py#L48-L56

Describe the solution you’d like

Add support for passing callable non-function-objects as callbacks to AsyncIOScheduler by modifying util.iscoroutinefunction to something like

def iscoroutinefunction_partial(f):
    while isinstance(f, partial):
        f = f.func

    return iscoroutinefunction(f) or (hasattr(f, '__call__') and iscoroutinefunction(f.__call__))

This does the trick.

Describe alternatives you’ve considered TBH, I don’t see any alternative solutions for this in terms of what APS could do. Ofc an alterantive would be for me to re-design my scheduling. That’s probably feasible and not too hard. But this use case seemed reasonable enough for me to open an issue 🙃

Additional context If there is a chance for another 3.x release and you would be okay with including this, I’m happy to try & PR with the solution proposed above. I’m aware that development is currently headed for v4. I haven’t tried finding out if this is an issue on v4 as well or how it could be supported. If you agree with adding support for is in general, I can try to investigate v4 in this regard.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
agronholmcommented, Jan 2, 2022

I think it would be reasonable to assume that this should work. I will add a test to ensure that it does.

1reaction
EveryTiancommented, Aug 29, 2022

Is there any follow-up on this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event Loop — Python 3.11.1 documentation
The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run...
Read more >
APScheduler 3.9.1.post1 - PythonFix.com
AsyncIOExecutor and class-based job callbacks ; How to connect to redis cluster in RedisJobStore; Storing extra columns than just job id, ...
Read more >
python asyncio run in executor with callback - Stack Overflow
I am pretty new with async programming and python in general but I want to implement an asynchronous function inside a state machine....
Read more >
Job Callbacks - Getting Started - NeverBounce
Job callbacks allow your application to react to status changes to your NeverBounce jobs. During job creation you can specify a URL for...
Read more >
Determinants of Callbacks to Job Applications: An Audit Study
Determinants of Callbacks to Job Applications: An Audit Study by Henry S. Farber, Dan Silverman and Till von Wachter. Published in volume 106,...
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