请求是一个python包,这使得执行HTTP请求非常容易。为了异步进行,您可以利用
Celery
. 对于芹菜设置,您可以按照
docs
. 你可以用
redis
作为经纪人。
在应用程序中创建任务。
# proj/tasks.py
from __future__ import absolute_import, unicode_literals
from .celery import app
import requests # https://github.com/requests/requests
@app.task
def call_api():
r = requests.get('https://api.github.com/events')
在您想调用函数的任何位置的文件中,例如在您的视图中
# proj/views.py
from tasks import call_api
call_api.delay()