【发布时间】:2019-01-13 12:58:40
【问题描述】:
我有这个方法:
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 文件。
或者,也许芹菜不适合这种情况?
有什么想法吗?
【问题讨论】:
-
首先:你不需要 celery,你的操作系统已经有一个调度器。第二;创建/更新模型的 ciode 应该存在于管理命令中(如果您决定使用操作系统调度程序)或 celery 任务(如果您真的想为此使用 celery),而不是在视图中。最后:GET 请求必须是幂等的(必须注意更改服务器的状态)。