2013-11-20 101 views
0

我用下面的代碼返回PDF文件wkhtmltopdf從HTML頁面在Django:生成PDF掛間歇性使用與IIS wkhtmltopdf在Windows Server 2008

currentSite = request.META['HTTP_HOST'] 

    params = { 'idOrganisation': idOrganisation, 'idMunicipalite' : idMunicipalite, 'nomMunicipalite' : nomMunicipalite, 'idUe': idUe, 'dateEvenement': dateEvenement} 

    command_args = "wkhtmltopdf -s A4 http://%s/geocentralis/fiche-role/propriete/?%s -" % (currentSite, urlencode(params)) 

    process = Popen(command_args.split(' '), stdout=PIPE, stderr=PIPE) 

    rtn_comm = process.communicate() #better than wait this wait and return for us... 
    pdf_contents = rtn_comm[0] #if want debug, index 1 return the stderror 

    r = HttpResponse(pdf_contents, mimetype='application/pdf') 
    r['Content-Disposition'] = 'filename=fiche-de-propriete.pdf' 

    return r 

的代碼工作,在2-3秒後生成pdf,但經常(間歇性地)生成pdf,它在生成pdf和螢火蟲前顯示爲「NetworkError:408 Request Timeout」之前約30-60秒。在此「掛起」期間,我的Django站點沒有迴應任何請求

我在Windows服務器2008上使用Django和IIS。

我在尋找如何解決這個問題的任何線索......

回答

1

它掛起的原因是因爲服務器運行到賽車/併發問題,並擊中死鎖(和你很可能使用相當受歡迎的資產或兩個在您的HTML)。

您請求PDF,所以服務器啓動wkhtmltopdf,它開始攪動您的PDF文件。當它到達一個資產(圖像,CSS或JS文件,字體等)時,wkhtmltopdf嘗試從該服務器加載它......這正好運行在同一臺服務器wkhtmltopdf上。如果服務器不能同時處理多個請求(或者不能很好地處理併發),則會進入死鎖狀態:wkhtmltopdf正等待正在等待wkhtmltopdf完成處理的服務器上的資產,以便它可以服務於資產wkhtmltopdf這是在資產等待...

要在開發中解決這個問題,只是Base64-embed your assets into the HTML轉換爲PDF,或暫時從另一臺機器(如臨時AWS桶)爲這些文件。這不應該是生產環境中的問題,因爲您的在線服務器(希望)能夠處理多個GET請求和線程。