Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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 是否有一种方法可以先返回响应,但在响应之后仍执行其他操作?德扬戈_Python_Django_Return_Response - Fatal编程技术网

Python 是否有一种方法可以先返回响应,但在响应之后仍执行其他操作?德扬戈

Python 是否有一种方法可以先返回响应,但在响应之后仍执行其他操作?德扬戈,python,django,return,response,Python,Django,Return,Response,我想首先在django视图中返回一个响应,然后在响应之后执行一些操作 假设我有这样的例子 class Res(View): def post(self, request): data = request.POST new_obj = Model.objects.create(name=data.['name']) # what is below does not have to be done RIGHT AWAY, can be do

我想首先在django视图中返回一个响应,然后在响应之后执行一些操作

假设我有这样的例子

class Res(View):
    def post(self, request):
        data = request.POST
        new_obj = Model.objects.create(name=data.['name'])

        #  what is below does not have to be done RIGHT AWAY, can be done after a response is made
        another_obj = Another()
        another_obj.name = new_obj.name
        another_obj.field = new_obj.field
        another_obj.save()

        # some other looping and a few other new models to save

        return JsonResponse({'status': True})
所以我想知道是否有机会先回复?以上是我的意思的一个例子

我不确定这是否可以在django完成,如果可能,有人能告诉我谁可以完成吗


提前感谢。

好吧,这更像是一个Python问题,而不是Django问题。正如评论所指出的,您可以实现某种异步队列,比如芹菜,但是对于您的用例来说,这可能有点过分了

考虑改用普通的:


这里的想法是将要异步运行的代码提取到函数中,并在线程中运行它们。

好吧,这更像是一个Python问题,而不是Django问题。正如评论所指出的,您可以实现某种异步队列,比如芹菜,但是对于您的用例来说,这可能有点过分了

考虑改用普通的:


这里的想法是将要异步运行的代码提取到函数中,并在线程中运行它们。

您可以使用芹菜任务来完成此任务。是的,芹菜是一种很好的方法。在这里阅读:thx thx,我很快就会尝试一下,看看它是如何工作的。通过阅读它,我可以认为我会把需要延迟的东西变成一个函数,然后
。延迟
它,但我真的不知道它什么时候知道调用它。你可以使用芹菜任务来完成这个任务。是的,芹菜是一种很好的方法。在这里阅读:thx thx,我很快会尝试一下,看看它是如何工作的。通过阅读,我可以认为我会把需要延迟的东西变成一个函数,然后
。延迟
它,但我真的不知道它什么时候知道调用它?我刚刚看到你的反应,这似乎是个好主意,我想试试。
线程(target=)
必须是函数吗?除了在这种情况下使用之外,还有一些其他情况,例如将对象传递给S3,而S3不需要这样做才能完成
JsonResponse
done。是的,
target
必须是一个函数。您还可以创建一个类,其中继承自
Thread
并重写
run()
。我刚才看到了您的响应,这似乎是个好主意,我想尝试一下。
线程(target=)
必须是函数吗?除了在这种情况下使用之外,还有一些其他情况,例如将对象传递给S3,而S3不需要这样做才能完成
JsonResponse
done。是的,
target
必须是一个函数。您还可以创建一个类,在该类中继承自
Thread
并重写
run()
from threading import Thread


def create_another_obj(name, field):
        another_obj = Another()
        another_obj.name = name
        another_obj.field = field
        another_obj.save()

class Res(View):
    def post(self, request):
        data = request.POST
        new_obj = Model.objects.create(name=data['name'])

        # start another thread to do some work, this is non-blocking
        # and therefore the JsonResponse will be returned while it is
        # running!
        thread = Thread(
                     target=create_another_obj,
                     args=(new_obj.name, new_obj.field),
                 )
        thread.start()

        return JsonResponse({'status': True})