首页 > 解决方案 > Django Rest Framework的HTTP循环内的异步HTTP调用

问题描述

我需要在views.py上我的一个视图集的get_queryset函数上调用外部服务,但我不需要等待外部函数的响应。

如何在不等待结果的情况下触发以下 requests.post 行并传递到下一行?

class SomeViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        ....
        requests.post(urlOfApi+token)
        .....
        return Some.objects.all()

标签: pythondjangoasynchronousdjango-rest-frameworkpython-asyncio

解决方案


我实现了一个端点来运行后台任务。当您点击post端点时,将返回作业的 ID。然后,您可以get使用作业任务 ID 访问端点,然后返回结果或者in progress作业是否仍在运行。

我没有celery用来完成这项任务,但我发现django-q比 celery 更容易,而且 django-q 使用了 django 已经拥有的所有电池。这是我用 django-q 实现的 API 视图示例。

from django.urls import reverse
from django_q.models import Task, OrmQ
from django_q.tasks import async, result
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from rest_framework.views import APIView


def the_function_to_run():
    # do something here
    pass


class YourTaskApiView(APIView):

    def get(self):

        # you mustr construct the get url to take the id
        task_id = self.kwargs.get('task_id')

        try:
            task = Task.objects.get(id=task_id)
        except Task.DoesNotExist:
            task = None

        # here the task is on processing
        if task:
            return Response({
                'id': task_id,
                'result': result(task_id),
                'started': task.started,
                'stopped': task.stopped,
                'status': 'DONE' if task.stopped else 'RUNNING',
                'success': task.success,
            })
        else:
            # here you find the task in the query (not even processed, but waiting for the cluster to process it)
            for q in OrmQ.objects.all():
                if q.task_id() == task_id:
                    task = q.task()
                    return Response({
                        'id': task['id'],
                        'started': task['started'],
                        'status': 'WAITING', # or ON QUEUE
                        'stopped': None,
                        'success': None,
                    })

        return NotFound()


    def post(self, request):

        # run the task as async
        task_id = async(the_function_to_run)

        # create the reverse for the get so the client already has the link to query for
        # the task status
        link = reverse('yourapp:yournamespace:yourview', kwargs={'task_id': task_id}, request=request)
        return Response(data={'task_id': task_id, 'link': link})

推荐阅读