Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Selik Newsgroups: comp.lang.python Subject: Re: python - handling HTTP requests asynchronously Date: Tue, 10 May 2016 23:54:47 +0000 Lines: 57 Message-ID: References: <59a499d5-13e1-4092-895f-34aca0827525@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de kb755iT/nQ5zLSRuAAJMPwu9c+IElHKDXQXFNSISj5kw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.04; 'binary': 0.05; 'concurrently': 0.07; 'wrapped': 0.07; 'wrapper': 0.07; 'api': 0.09; 'concurrent': 0.09; 'retry': 0.09; 'subject:skip:a 10': 0.09; 'threads.': 0.09; 'django': 0.13; 'subject:python': 0.14; 'suggest': 0.15; 'file,': 0.15; 'user.': 0.15; '2016': 0.16; 'backoff': 0.16; 'expert.': 0.16; 'folder.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:handling': 0.16; 'subject:requests': 0.16; 'threads': 0.16; 'user?': 0.16; 'wrote:': 0.16; 'module,': 0.18; '>': 0.18; 'email addr:gmail.com>': 0.18; 'background.': 0.20; 'to:2**1': 0.21; 'sat,': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; 'rest': 0.26; 'external': 0.27; 'skip:m 30': 0.27; '(e.g.': 0.27; 'checking': 0.27; 'fri,': 0.27; 'separate': 0.27; 'to:no real name:2**1': 0.27; 'message-id:@mail.gmail.com': 0.27; 'interface': 0.29; 'queue': 0.29; 'handled': 0.29; "i'm": 0.30; 'code': 0.30; 'e.g.': 0.30; 'post': 0.31; 'possibly': 0.32; 'returned': 0.32; 'maybe': 0.33; 'third': 0.33; 'michael': 0.33; 'http': 0.33; 'file': 0.34; 'info': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'possible,': 0.35; 'reply.': 0.35; 'but': 0.36; 'too': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'pdf': 0.37; 'doing': 0.38; 'progress': 0.38; 'received:209': 0.38; 'stuff': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'easy': 0.60; 'care': 0.60; 'your': 0.60; 'back': 0.62; 'goal': 0.64; 'periodically': 0.66; 'reuse': 0.66; 'saving': 0.70; 'sounds': 0.76; '100': 0.79; 'payload': 0.84; 'pdfs': 0.84; 'sabato': 0.84; 'sending,': 0.84; 'generation.': 0.91; 'request:': 0.91; 'response,': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=7uH1Ow5yraCNOXSw5JZ0udPVeUBhYu6voQusDSepKxo=; b=P6z+DhG/FeLLaVSj0NUiEDfsJeoo899PiyuXijGqfoFklwjtfw/kXztS9D+E5yD+SU JfeXlkjtItBMhUiZswDC1209gY6CmmHasdNsdjfl79AEHzBf2gaKoudBPDTLAcD4zLPe n0WmMjRD1HgyiYgViTiVOWraAgb740xSXWz8CjbLMYWHwyvoAYCUSM45JfWivhquLLOc 1b70AWgdz+u0oooiQTciKAm2WuQOE67fBXWPm2q5xGrJFBcczJvQap8HA+Te9FpGfBHR +ip8LZapFR0rypfT9egEu2pxJFEn9jWUrlA4ZTPXfsDSpYgLEvv6J9sIQa0TPgeiGfa9 0DTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=7uH1Ow5yraCNOXSw5JZ0udPVeUBhYu6voQusDSepKxo=; b=ivcjN1f5YWMuPxiUu7a+fX7UeDVJ4EQijkg6negUzLN1ZsiBYuIVMmtbbh4lk2kkjA 0FIk17zPgxaPo+JL2ExzPOPwR00nqBeR2THGcrugPoDuS5HaDndqs/RL4fRiNOEXS3rh loSfPWpg8nO4Jn0pYSQMafLeG/2UjQ+Ca8HDB9mQ3QIkLGl4It+OSdndcJjF2rinZMDd 7Jf4GTlyPg4Rl3VT/l0me/VPhGyMFVakTJFg5Yl1Cxr2kEpyKG0C6u/JNJHaJ4C6HH3s kdCl752c9LczsnuHDBQnOF5eNd3aujFecHNSSnxkHb3MBrcWCFHF2VOW1h8fJkSXpWda yA9Q== X-Gm-Message-State: AOPr4FXoS4gEzAS9G97DfUvbhQVcjqvv/DftsmNiue/fLPzV/O/4j7H6fqalE+AvQ48bI3w3WgAgUEDZTJHrVw== X-Received: by 10.140.108.247 with SMTP id j110mr347485qgf.54.1462924497241; Tue, 10 May 2016 16:54:57 -0700 (PDT) In-Reply-To: <59a499d5-13e1-4092-895f-34aca0827525@googlegroups.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <59a499d5-13e1-4092-895f-34aca0827525@googlegroups.com> Xref: csiph.com comp.lang.python:108503 On Sat, May 7, 2016 at 4:46 PM wrote: > Il giorno sabato 7 maggio 2016 21:04:47 UTC+2, Michael Selik ha scritto: > > On Fri, May 6, 2016 at 3:01 AM wrote: > > > > > The PDF is generated through an external API. Since currently is > generated > > > on demand, this is handled synchronously via an HTTP request/response. > > > > > > Are you sending the request or are you receiving the request? > > If you are sending, you can just use threads as you are only doing IO. > > If you are receiving the requests and generating PDFs, you may want to > use > > subprocesses if the PDF-generation is compute-intensive. > > > > > > > 3) multiprocessing module, with e.g. 10 as workers limit. > > > > > > > multiprocessing.Pool is an easy way to use subprocesses > > multiprocessing.pool.ThreadPool is an easy way to use threads. It's not > > well documented, but has the exact same interface as Pool. > > > > the goal is to use the API concurrently (e.g. send 10 or 100 http > requests > > > simultaneously, depending on how many concurrent requests the API can > > > handle). > > > > > > > Sounds like you want to use threads. How does the API let you know you're > > hitting it too frequently? Perhaps you want to code an exponential > backoff > > and retry wrapper for your API requests. > > Thanks for the reply. > Currently the django view that does the job does three http request: > - the first does a POST and send the payload used during the PDF > rendering, the response contains a url to check the pdf generation progress; > - the second loop over that url, checking the progress of pdf generation. > Once the response contains the keyword 'status': 'complete', then it give > also a url for the file retrieval; > - the third one is a GET to retrieve the file, the reponse contains the > binary content of the file, then this content is read and wrapped as > attachment of a django http response, and then returned to the user. > > the goal is to reuse the existing code as much as possible, possibly doing > concurrently, and saving the file instead on a folder. > I'm not a Django expert. Does the Django View require all that stuff to happen before Django can send an HTTP Response back to the user? If so, I suggest you respond to the user immediately: "now generating a bunch of PDFs..." and take care of the rest in the background. Perhaps just write to a file, database, or message queue the info for the PDFs to generate. Have a separate program periodically read the file, database, or message queue to do that work and then maybe email the user when it's completed.