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 FlaskWorker #119

Open
tonycpsu opened this issue Feb 18, 2022 · 0 comments
Open

Add FlaskWorker #119

tonycpsu opened this issue Feb 18, 2022 · 0 comments

Comments

@tonycpsu
Copy link

flask_rq2.job.FlaskJob is nice, but initializing the app each time can be time-consuming if the app has to make a database connection, initialize some other extensions, etc. It'd be great if this were handled by a custom Worker class. I gave it a try and while I haven't tested extensively this seems to work:

class FlaskWorker(Worker):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.script_info = ScriptInfo()
        self.app = self.load_app()

    def perform_job(self, job, queue):
        with self.app.app_context():
            return super().perform_job(job, queue)

    def load_app(self):
        if current_app:
            app = current_app
        else:
            app = self.script_info.load_app()
        return app

If this seems useful to anyone else I'd be happy to submit a PR.

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