Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 异步长时间运行的操作API调用_Python_Reactjs_Django_Async Await - Fatal编程技术网

Python 异步长时间运行的操作API调用

Python 异步长时间运行的操作API调用,python,reactjs,django,async-await,Python,Reactjs,Django,Async Await,我的react应用程序中有一个函数,可以将数据发送到Django API。收到数据后,django调用一个外部python函数来执行一些代码 目前,我让javascript在收到ok响应时向我发出警报。但是,Django在外部函数完成之前不会发送此响应;这是一个问题,因为根据用户的输入,运行外部功能可能需要一个小时。当外部python代码开始成功运行并且函数完成后第二次运行时,是否可以将此更改为发出一个警报 我理解,当将数据发送到API时,它们可能会失败,API无法访问数据可能是因为数据类型不匹

我的react应用程序中有一个函数,可以将数据发送到Django API。收到数据后,django调用一个外部python函数来执行一些代码

目前,我让javascript在收到
ok
响应时向我发出警报。但是,Django在外部函数完成之前不会发送此响应;这是一个问题,因为根据用户的输入,运行外部功能可能需要一个小时。当外部python代码开始成功运行并且函数完成后第二次运行时,是否可以将此更改为发出一个警报

我理解,当将数据发送到API时,它们可能会失败,API无法访问数据可能是因为数据类型不匹配,最后,如果数据与外部函数不兼容。我正在从异步函数中寻找3种不同的响应 反应

models.py

class DataInput(models.Model):
    data_one = models.IntegerField(
        max_length=30,
        default=5)
    data_two = models.IntegerField(
        max_length=30,
        default=4)

class OtherData(models.Model):
    other_data = models.IntegerField(
        max_length=5,
        default=10)

@receiver(post_save, sender=DataInput, dispatch_uid="extra function")
def extra_function(sender, instance, created, *args, **kwargs):
    #dummy function to show reliance on data
    for i in OtherData[0].other_data:
        print(instance.data_two + instance.data_one)
序列化程序.py

from rest_framework import serializers
from .models import DataInput
from .models import OtherData
class DataSerializer(serilizers.ModelSerializer):
    class Meta:
        model = DataInput
        fields = ('data_one', 'data_two')
class OtherDataSerializer(serializer.ModelSerializer):
    class Meta:
        model = OtherData
        fields = ('other_data')

最好避免在同一(http)连接中同步等待长时间运行的操作的响应。特别是在浏览器中,因为它们可能是超时的原因(取决于浏览器,但是)

解决方案之一是使用HTTP轮询。有关此技术的详细说明,请参见:


正如您在第二个链接中所看到的,在启动长时间运行的流程之前,您的API应该验证请求和要执行的操作。如果请求无效,请立即回复错误代码,如HTTP 400(错误请求)。在另一种情况下,将任务交给后台工作人员(例如),使用react应用程序可以轮询等待作业结果的位置进行响应。

最好避免在同一(http)连接中同步等待长时间运行的操作的响应。特别是在浏览器中,因为它们可能是超时的原因(取决于浏览器,但是)

解决方案之一是使用HTTP轮询。有关此技术的详细说明,请参见:

正如您在第二个链接中所看到的,在启动长时间运行的流程之前,您的API应该验证请求和要执行的操作。如果请求无效,请立即回复错误代码,如HTTP 400(错误请求)。在另一种情况下,将任务交给后台工作人员(例如),使用react应用程序可以轮询等待作业结果的位置进行响应

from rest_framework import serializers
from .models import DataInput
from .models import OtherData
class DataSerializer(serilizers.ModelSerializer):
    class Meta:
        model = DataInput
        fields = ('data_one', 'data_two')
class OtherDataSerializer(serializer.ModelSerializer):
    class Meta:
        model = OtherData
        fields = ('other_data')