gpt4 book ai didi

python - Django runserver 在等待查询服务器的外部 Web 服务时挂起

转载 作者:行者123 更新时间:2023-11-30 23:45:46 24 4
gpt4 key购买 nike

我在 Django 中有第一个 View ,它调用第三方 Web 服务将给定 URI 处的 HTML 页面转换为 PDF。 (具体来说,pdfcrowd)。我提供 Web 服务的 URI 对应于第二个 Django View 。因此,当我从第一个 View 调用第三方 Web 服务时,Web 服务会向我的服务器发送对 url(对应于第二个 View )的页面的请求,以便将生成的 HTML 转换为 PDF 并返回表示第一个 View 的 PDF 文件的字节。 (见下面的代码)

但是,Django runserver 挂起了这个问题,我假设它是因为它不执行并行执行,并且在第一个 View 仍在执行和等待时无法处理一个请求(对应于第二个 View )。我还假设这在我的生产服务器中运行良好,该服务器运行 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:
. . .

最佳答案

django-devserver提供线程化的runserver命令替换,支持并行执行。

可能,runserver_plusdjango-extensions提供也是 - 它的动力来自 werkzeug并包括一个令人惊叹的回溯页面。

关于python - Django runserver 在等待查询服务器的外部 Web 服务时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9390310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com