Django Rest Fram的HTTP循环内的异步HTTP调用

2024-10-01 07:30:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我该怎么做请求.post行刚刚触发并传递到下一行而不等待结果?在

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

Tags: 函数pyself视图getdefpostrequests
2条回答

我实现了一个端点来运行后台任务。当您点击post时,端点返回作业的id。然后,您可以使用作业任务id命中get端点,并返回结果,如果作业仍在运行,in progress。在

我没有使用celery来完成这项任务,但是我发现{a1}比芹菜更容易,而且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})

你需要为这类工作实现一些后台工作。在

正如评论中提到的-一种方法是使用芹菜。在

如果你不想走那条路。您可以尝试以下操作:

https://django-background-tasks.readthedocs.io/en/latest/

也许,没有芹菜的方式强大,但更容易设置和运行。在

您只需实现一个函数来执行这项工作(在您的例子中,向另一个服务发出http请求),用后台装饰器装饰它。 然后,在视图集中,简单地调用函数,它就会被调度。你甚至可以选择时间。在

相关问题 更多 >