apscheduler

Question 1: How to start a scheduled task when the Django service starts
? To prepare to use APscheduler in Django, first start a task in the wsgi module

from apscheduler.schedulers.background import BackgroundScheduler

scheduler = BackgroundScheduler()

Let Django generate the scheduler service when the service starts. .
However, after the business module has written the timed api and scheduler.start() to be called, there is no response. It was not until later that the business module was imported in the urls.py module
before the timed task was realized!

Summarize past experience and guess: the first step in Django service startup is to load the wsgi module, and then start to load the urls module. As for whether it is correct, I can only study it later, and I didn't find it well on the official website!

————The dividing line————It
seems that the urls.py module does not work either. I made it in the environment of Django1.8. I ran to the wsgi module again today

——Dividing line——
I tried it on Mac today, and the command python manage.py runserver can be implemented in both urls and wsgi. Wonderful!

Question 2: How to add apscheduler's job to the database
At the beginning, when configuring, I thought that msyql could not be used to store tasks. Later, I found that SQLAlchemyJobStore in jobstores can be implemented, not only mysql, sqlite3 also share this set,
and in When configuring, pay attention to how to configure (there are details in the source code):

'default':SQLAlchemyJobStore(url='mysql://root:[email protected]:3306/djangomysql'

Since apscheduler is used, the decorator must also be used:
find examples on two websites:
https://segmentfault.com/a/1190000004238416
http://python.jobbole.com/82344/

No parameters or return value

def deco(func):
    def _deco():
        print 'before invoked'
        func()
        print 'after invoked'
    return _deco

@deco
def f():
    print 'f is invoked'

If the decorated function f takes parameters and has a return value

def deco(func):
    def _deco(*args, **kwargs):
        print 'before invoked'
        ret = func(*args, **kwargs)
        print 'after invoded'
        return ret
    return _deco

@deco
def f(a):
    print 'f is invoked'
    return a + 1

If the decorator has parameters, it needs to be wrapped one more layer, and the parameter call is wrapped in it

def deco(*args):
    def _deco(func):
        def __deco(*args, **kwargs):
            print 'decorator args is', args
            print 'before invoked'
            ret = func(*args, **kwargs)
            print 'after invoded'
            return ret
        return __deco
    return _deco

@deco('test')
def f(a):
    print 'f is invoked'
    return a + 1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815276&siteId=291194637