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

Can we run multiple APScheduler jobs parallely? #38

Closed
mrabhiram opened this issue Mar 2, 2017 · 6 comments
Closed

Can we run multiple APScheduler jobs parallely? #38

mrabhiram opened this issue Mar 2, 2017 · 6 comments

Comments

@mrabhiram
Copy link

The way I see it right now, if i define two or more jobs, they are running sequentially. Is there a way we can run multiple jobs parallely?

class Config(object):
    JOBS = [
        {
            'id': 'job1',
            'func': 'jobs:job1',
            'args': (1, 2),
            'trigger': 'interval',
            'seconds': 10
        },
       {
            'id': 'job2',
            'func': 'jobs:job1',
            'args': (3, 4),
            'trigger': 'interval',
            'seconds': 10
        }
    ]

    SCHEDULER_API_ENABLED = True


def job1(a, b):
    print(str(a) + ' ' + str(b))

if __name__ == '__main__':
    app = Flask(__name__)
    app.config.from_object(Config())

    scheduler = APScheduler()
    # it is also possible to enable the API directly
    # scheduler.api_enabled = True
    scheduler.init_app(app)
    scheduler.start()
    app.run()

Right now the two jobs defined(job1 and job2) are running one after another. Is there a way to execute them individually and parallely?

@viniciuschiele
Copy link
Owner

viniciuschiele commented Mar 2, 2017

Hi @mrabhiram

Your jobs above are running parallely, each job runs on its own thread, the default is 10 max concurrent threads.

This code below (python 3+) shows that each job is running in a different thread.

import threading

def job1(a, b):
    print(threading.current_thread().name)
    print(str(a) + ' ' + str(b))

@mrabhiram
Copy link
Author

Okay, Thank you. Can i change the max concurrent threads? if so , where do i do that?

@viniciuschiele
Copy link
Owner

You have to explicitly set an executor in your config object.

class Config(object):
    JOBS = [
        {
            'id': 'job1',
            'func': 'jobs:job1',
            'args': (1, 2),
            'trigger': 'interval',
            'seconds': 10
        },
       {
            'id': 'job2',
            'func': 'jobs:job1',
            'args': (3, 4),
            'trigger': 'interval',
            'seconds': 10
        }
    ]

    SCHEDULER_EXECUTORS = {
        'default': {'type': 'threadpool', 'max_workers': 20}
    }

    SCHEDULER_API_ENABLED = True

@mrabhiram
Copy link
Author

Thank you. Can many workers is the max that APScheduler can handle? Or the max is only 20?

@viniciuschiele
Copy link
Owner

There isn't a limit for the number of workers, it's up to you.

@mrabhiram
Copy link
Author

Awesome. Thanks!

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

2 participants