python-telegram-bot icon indicating copy to clipboard operation
python-telegram-bot copied to clipboard

[FEATURE] Allow manual selection of APScheduler scheduler backend (e.g. for better Twisted compatibility)

Open critx-jeti opened this issue 5 years ago • 5 comments

Is your feature request related to a problem? Please describe.

When attempting to integrate the python-telegram-bot in my existing twisted application, I noticed that it makes use of APScheduler. In the application, I then had two concurrent frameworks running, the Twisted reactor as well as the APScheduler BackgroundScheduler, which leads to decreased performance and increases the risk for race conditions.

Since APScheduler provides native support for Twisted integration, I wanted to enable it by switching to the built-in TwistedScheduler. Unfortunately, this option is not provided by the current implementation of python-telegram-bot.

Describe the solution you'd like

The APScheduler is imported and used in jobqueue.py exclusively. Instead of hardcoding the BackgroundScheduler, we could enable the user to pass an optional named parameter when initializing a JobQueue.

The current behavior should stay as default behavior. Users which have the need for a different scheduler must then

  • import the scheduler in their code
  • initilize the scheduler in their code
  • initialize the JobQueue themselves and pass the scheduler reference

Describe alternatives you've considered

There is a multitude of schedulers provided by APScheduler: https://apscheduler.readthedocs.io/en/stable/search.html?q=schedulers Alternatives include schedulers employing the first-party asyncio library (PEP 3156), gevent, Twisted and Tornado. These are drop-in replacements and do not require additional configuration on our side.

Additional context

Making the APScheduler entirely optional was discussed in https://github.com/python-telegram-bot/python-telegram-bot/issues/2257 already, but that issue was closed without removing the dependecy. I propose an alternate solution that allows for integration with similar projects without having to write a lot of additional code.

critx-jeti avatar Jan 08 '21 16:01 critx-jeti

Hi, thanks for reaching out. Passing a custom scheduler indeed sounds like a rather simple addition. However I must admit that I'm a bit reluctant as it's not quite clear to me if it would have any implications on PTB and it feels a bit like giving the user the gun to shoot themself in the foot.

Are you in fact using the extensions module telegram.ext and just would like the JobQueue to work differently or is your request actually similar to #2257, i.e. a possibility to install ptb without telegram.ext?

For the moment, I guess subclassing JobQueue and overriding it's __init__ (coyping from the current implementation & replacing the scheduler) would be enough, to get things to work.

Bibo-Joshi avatar Jan 08 '21 18:01 Bibo-Joshi

Hello @Bibo-Joshi, thanks for the fast answer. I am indeed using the extensions. The only non-extensions example I could find was rawapibot.py and I deemed it to be simpler to just use telegram.ext.

Locally, I am using a fork of v13.1 where I just swapped out the hardcoded scheduler. This is working fine for me and it detects the standard Twisted reactor without any additional effort, effectively solving this problem for me. Subclassing would of course also work and void the need for the fork.

I can understand your reluctance with regard to possible implications. I am not familiar enough with the inner workings of APScheduler to confirm that this will have no implications, otherwise I would have just submitted a pull request. This might result in obscure unwanted behavior, though I guess that burying this option far enough to require multiple voluntary actions (as described in the initial report) should at least give users an idea what might have caused it.

critx-jeti avatar Jan 08 '21 18:01 critx-jeti

burying this option far enough to require multiple voluntary actions should at least give users an idea what might have caused it.

True. A big note should probably do the trick. It should also be mentioned (and implemented), that the scheduler will be configured to use pytz.utc by default, if the bot has no other setting.

Still, I'm not 100% happy with giving documented access to an untested feature, so I'm just brainstorming for alternatives. One would be to make the used scheduler class as class attribute, i.e. JobQueue.SCHEDULER_CLASS = BackgroundScheduler and document it. Then advanced users as yourself can easily create a custom subclass with minimal effort

MyJobQueue(JobQueue):
    SCHEDULER_CLASS = CustomScheduler

That would be a little bit more like leaving the door to the possibly haunted house open, but not putting a sign up :D

Bibo-Joshi avatar Jan 09 '21 19:01 Bibo-Joshi

Uh this is the kind of behaviour we allow with urrlib3, I would vote in favour of implementing it like that; with a warning that they are entering dangerous areas and have to do so without our support.

Poolitzer avatar Jan 10 '21 16:01 Poolitzer

This issue came up in dev chat again and we came to the conclusion that we're not entirely opposed, but also don't see it on any roadmap in the forseable future. Hence, I'll close. If this feature would benefit you significantly and subclassing JobQueue as described above doesn't do the trick for you, you're welcome to open a new issue (please make sure to reference this one) or send a PR.

Bibo-Joshi avatar Apr 13 '21 08:04 Bibo-Joshi