使用 Celery 调度 Django 方法

我有这个方法:


def getExchangeRates(): 

    """ Here we have the function that will retrieve the latest rates from fixer.io """

    rates = {}

    response = urlopen('http://data.fixer.io/api/latest?access_key=c2f5070ad78b0748111281f6475c0bdd')

    data = response.read()

    rdata = json.loads(data.decode(), parse_float=float) 

    rates_from_rdata = rdata.get('rates', {})

    for rate_symbol in ['USD', 'GBP', 'HKD', 'AUD', 'JPY', 'SEK', 'NOK']:

        try:

            rates[rate_symbol] = rates_from_rdata[rate_symbol]

        except KeyError:

            logging.warning('rate for {} not found in rdata'.format(rate_symbol)) 

            pass

    return rates


@require_http_methods(['GET', 'POST'])

def index(request):

    rates = getExchangeRates()

    fixerio_rates = [Fixerio_rates(currency=currency, rate=rate)

                 for currency, rate in rates.items()]

    Fixerio_rates.objects.bulk_create(fixerio_rates)

    return render(request, 'index.html') 

我想安排这个,比方说,每天早上 9 点,周末除外。


我还没有找到一些关于如何基于这样一个特定的日期时间来安排这个的综合教程,而且,我不知道我是否可以安排这个方法,或者在我的tasks文件中创建另一个继承这个方法的方法,并在任何时间运行具体日期。


我的celery.py项目根目录中有该tasks.py文件,我的应用程序文件夹中有该文件。


或者,也许芹菜不是解决这种情况的方法?


有任何想法吗?


qq_笑_17
浏览 158回答 3
3回答

慕森卡

此外,您可以使用Celery Beat来安排您需要的任务。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python