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.

Apscheduler as a class member

See original GitHub issue

Hi, I’m trying to create a simple class with apscheduler with specific jobstore suppose this simple example:


from datetime import datetime
from time import sleep

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore


class sched:
    def __init__(self):
        jobstores = {
            'default': SQLAlchemyJobStore(url='sqlite:///test.db')
        }
        self.scheduler = BackgroundScheduler(jobstores=jobstores)
        job2 = self.scheduler.add_job(self.tick, 'interval', args=[1, 2], kwargs=dict(a=2,b=3), seconds=5, replace_existing=True)
        self.scheduler.start()

    def tick(self, i, j, **kwargs):
        print("hello{} {}".format(i, j))
        for k in kwargs:
            print(k, ":", kwargs[k])



if __name__ == '__main__':
    #print(scheduler.get_jobs())
    schedule = sched()
    while True:
        sleep(1)


but it gaves me this error:

TypeError: tick2() missing 1 required positional argument: ‘j’

but when I change the instance name with this it works fine:

if __name__ == '__main__':
    #print(scheduler.get_jobs())
    sched = sched()
    while True:
        sleep(1)

It works fine. I don’t have any problem with default jobstore. I don’t know if the problem relates to apscheduler or not but I’m a little confused here why should I use an instance name with the exact name of the class name. Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
agronholmcommented, May 20, 2018

What is a “schedule class” here? I can give advice if you show me some code but I am not some free consultancy service.

1reaction
agronholmcommented, Apr 29, 2018

Not to mention that starting the scheduler directly in __init__() is also a bad idea, particularly from the testability standpoint.

Read more comments on GitHub >

github_iconTop Results From Across the Web

apscheduler - import a class method from file as static in python
I'm trying to use the Apscheduler library. I created a class method for the library and added some missing features by myself as...
Read more >
User guide — APScheduler 3.9.1 documentation
APScheduler provides many different ways to configure the scheduler. You can use a configuration dictionary or you can pass in the options as...
Read more >
apscheduler.job — APScheduler 3.9.1 documentation
Contains the options given when scheduling callables and its current schedule and other state. This class should never be instantiated by the user....
Read more >
apscheduler.events - Read the Docs
An event that concerns the submission of a job to its executor. Variables. scheduled_run_times – a list of datetimes when the job was...
Read more >
Frequently Asked Questions — APScheduler 3.9.1 ...
from apscheduler.schedulers.background import BackgroundScheduler def ... to the module level or to class level as either a class method or a static method....
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