2012-02-22 63 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