Reputation: 42043
I have a first view in Django that calls a third-party web service to converts an HTML page at a given URI to a PDF. (pdfcrowd, to be specific). The URI that I provide the web service corresponds to a second Django view. Therefore, when I invoke the third-party web service from my first view, the web service sends a request to my server for the page at the url (corresponding to the second view) in order to convert the resulting HTML to PDF and return the bytes representing the PDF file to the first view. (See the code below)
However, Django runserver gets hung up on this, and I'm assuming its because it does not do parrallel execution and can't tackle one request (corresponding to the second view) while the first view is still executing & waiting. I'm also assuming that this will work fine in my production server, which runs Gunicorn, which should handle the parallel request processing just fine.
I'm wondering if there is a good work around to be able to use this code with runserver on my development machines.
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:
. . .
Upvotes: 1
Views: 1612
Reputation: 33420
django-devserver provides a threaded runserver command replacement, supporting parallel execution.
Probably, runserver_plus provided by django-extensions is too - it's powered by werkzeug and includes an amazing traceback page.
Upvotes: 3