3

是否有可能做这样的事情:

def new_query(request,company_uuid,address_uuid,contact_uuid):
    mcompany = get_object_or_404(Company, uuid=company_uuid)
    if request.method == 'POST': # If the form has been submitted...
        form = forms.CompanyQueryForm(request.POST)
        if form.is_valid():
            mquery = form.save(commit = False)
            mcompany = get_object_or_404(Company, uuid = company_uuid)
            mquery.company = mcompany
            mquery.version_number = 1
            mquery.parameters = {
                                    'company':company_uuid,
                                    'address':address_uuid,
                                    'contact':contact_uuid
                                    }
            mquery.save()
            preserialise(mquery.pk, company_uuid)
            recent_update = RecentUpdate(company_query=mquery, update_type="1")
            recent_update.save()
            url = reverse('view_directory',kwargs={'company_uuid':company_uuid,
                                                                'address_uuid':address_uuid,
                                                                'contact_uuid':contact_uuid})
            return HttpResponseRedirect(url)
    else:
        form = forms.CompanyQueryForm()
    return share.output_page(request,'joinerysoft/new_query.html',{'title':unicode(u'New Company Query'),
                                                                   'form': form,
                                                                   'company':mcompany,
                                                                   'address_uuid':address_uuid,
                                                                   'contact_uuid':contact_uuid})   

哪里preserialise(mquery.pk, company_uuid)在后台运行而不等待返回?因为预序列化需要很长时间才能完成(超过 5 分钟),从用户的角度来看,我希望它成为一场火灾并忘记。

4

2 回答 2

6

你总是可以触发一个线程:

import threading

class PreserializeThread(threading.Thread):
    def __init__(self, mquery_pk, company_uuid, *args, **kwargs):
        self.mquery_pk = mquery_pk
        self.company_uuid = company_uuid
        super(PreserializeThread, self).__init__(*args, **kwargs)

    def run(self):
        preserialize(self.mquery_pk, self.company_uuid)

然后,preserialize在您的代码示例中替换为:

PreserializeThread(mquery.pk, company_uuid).start()

另见:http ://docs.python.org/library/threading.html

于 2012-07-24T15:24:21.243 回答
1

简单的回答,不。

在继续执行之前,您的函数将持续到序列化完成。

查看django-celery以获得任务队列解决方案。

截至 2020 年,celery现在支持开箱即用的 Django。

于 2012-07-24T14:02:36.000 回答