python - 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()
解决方案
我实现了一个端点来运行后台任务。当您点击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})
推荐阅读
- c# - 当长度低于padleft的长度时,Padleft不在字符串中添加零?
- python - 在 y-achse 上找不到标签
- react-native - React Native 的“Slingshot”样式 UI 控件
- java - 编译为在 Java 6 上运行但在 Java 8 JVM 上运行的应用程序与在 Java 8 上编译的相同 App 之间的差异
- python - pip 是否将软件包安装到我的 conda 环境中?
- python - 在 SQL For 循环中迭代数组列
- java - Java:如何以用户身份执行外部命令?
- git - git push,现在我有一个分离的头
- javascript - HTTP GET Parameters not getting constructed by Angular 7.2 on IE 11
- c++ - 在 Qt 中将 QStringList 项目添加到 QComboBox 时遇到问题