Is there any way to make an asynchronous function call from Python [Django]?

前端 未结 4 413
余生分开走
余生分开走 2020-12-16 05:17

I am creating a Django application that does various long computations with uploaded files. I don\'t want to make the user wait for the file to be handled - I just want to s

4条回答
  •  盖世英雄少女心
    2020-12-16 06:13

    threading will break runserver if I'm not mistaken. I've had good luck with multiprocess in request handlers with mod_wsgi and runserver. Maybe someone can enlighten me as to why this is bad:

    def _bulk_action(action, objs):
        # mean ponies here
    
    def bulk_action(request, t):
    
        ...
        objs = model.objects.filter(pk__in=pks)
    
        if request.method == 'POST':
            objs.update(is_processing=True)
    
            from multiprocessing import Process
            p = Process(target=_bulk_action,args=(action,objs))
            p.start()
    
            return HttpResponseRedirect(next_url)
    
        context = {'t': t, 'action': action, 'objs': objs, 'model': model}
        return render_to_response(...)
    

    http://docs.python.org/library/multiprocessing.html

    New in 2.6

提交回复
热议问题