2012-02-22 32 views
1

我在Django中有第一個視圖,它調用第三方Web服務將給定URI處的HTML頁面轉換爲PDF。 (pdfcrowd,具體)。我提供Web服務的URI對應於第二個Django視圖。因此,當我從第一個視圖調用第三方Web服務時,Web服務會將請求發送到我的服務器以獲取URL(對應於第二個視圖)的頁面,以便將生成的HTML轉換爲PDF並返回字節表示PDF文件到第一個視圖。 (見下面的代碼)Django runserver在等待查詢服務器的外部Web服務時掛起

但是,Django runserver掛起了這個,我假設它是因爲它沒有執行並行執行,並且不能處理一個請求(對應於第二個視圖),而第一個查看仍在執行&正在等待。我還假設這可以在運行Gunicorn的生產服務器上正常工作,該服務器應該很好地處理並行請求處理。

我想知道是否有一個很好的解決方案可以在我的開發機器上使用runserver代碼。

class PdfMenuView(View): 
    def get(self, request, *args, **kwargs): 
     # I actually reverse a urlconf to get the full url, but show this as hardcoded for simplicity 
     prePdfHtmlUrl = "http://1.2.3.4:8080/url-to-convert/" # my router forwards 8080 to my machine to enable testing with external web services. 
     try: 
      # create an API client instance 
      client = pdfcrowd.Client(settings.PDFCROWD_USERNAME, settings.PDFCROWD_KEY) 

      # convert a web page and store the generated PDF to a string 
      pdf_string = client.convertURI(prePdfHtmlUrl) # this is where it gets hung up, since the client will cause the webserver to query the URI I provide it in order to get the page to convert, but this view is still tying up the runserver execution so the second view can't execute 
     except:  
      . . . 

回答

3

django-devserver提供螺紋的runserver命令置換,支持並行執行。

也許,通過django-extensions提供runserver_plus過 - 它是由werkzeug供電,包括驚人回溯頁。

+1

帶有--threaded參數的runserver_plus適合我!謝謝。我會使用django-devserver,但我不喜歡它如何替換runserver命令,因爲我將它與pycharm的嵌入式調試器結合使用,所以我更願意保留這一點。 – 2012-02-22 19:12:28