|
45 | 45 | <div class="row"> |
46 | 46 | <div class="col-md-8"> |
47 | 47 | <h1>Task queues</h1> |
48 | | -<p>Task queues handle background work processed outside the usual HTTP |
49 | | -request-response cycle. </p> |
| 48 | +<p>Task queues manage background work that must be executed outside the usual |
| 49 | +HTTP request-response cycle.</p> |
50 | 50 | <h2>Why are tasks queues necessary?</h2> |
51 | | -<p>Some tasks are handled asynchronously either because they are not initiated by |
52 | | -an HTTP request or because they are long-running jobs that take longer than |
53 | | -a few milliseconds. </p> |
| 51 | +<p>Tasks are handled asynchronously either because they are not initiated by |
| 52 | +an HTTP request or because they are long-running jobs that would dramatically |
| 53 | +reduce the performance of an HTTP response.</p> |
54 | 54 | <p>For example, a web application could poll the GitHub API every 10 minutes to |
55 | | -find out what are the top 100 starred repositories. A task queue would be set |
56 | | -up to automatically call the GitHub API, process the results and store them |
| 55 | +collect the names of the top 100 starred repositories. A task queue would |
| 56 | +handle invoking code to call the GitHub API, process the results and store them |
57 | 57 | in a persistent database for later use.</p> |
58 | 58 | <p>Another example is when a database query would take too long during the HTTP |
59 | 59 | request-response cycle. The query could be performed in the background on a |
60 | | -fixed interval with the results stored in the database. Then when the |
61 | | -HTTP request comes in it could fetch the calculated results from the database |
62 | | -instead of re-executing the query. This is a form of <a href="/caching.html">caching</a> |
63 | | -enabled by task queues.</p> |
| 60 | +fixed interval with the results stored in the database. When an |
| 61 | +HTTP request comes in that needs those results a query would simply fetch the |
| 62 | +precalculated result instead of re-executing the longer query. |
| 63 | +This precalculation scenario is a form of <a href="/caching.html">caching</a> enabled |
| 64 | +by task queues.</p> |
64 | 65 | <p>Other types of jobs for task queues include</p> |
65 | 66 | <ul> |
66 | 67 | <li> |
67 | | -<p>calculating computationally expensive data analytics</p> |
68 | | -</li> |
69 | | -<li> |
70 | | -<p>scheduling periodic jobs such as batch processes</p> |
71 | | -</li> |
72 | | -<li> |
73 | 68 | <p>spreading out large numbers of independent database inserts over time |
74 | | - instead of all at once</p> |
| 69 | + instead of inserting everything at once</p> |
75 | 70 | </li> |
76 | 71 | <li> |
77 | 72 | <p>aggregating collected data values on a fixed interval, such as every |
78 | 73 | 15 minutes</p> |
79 | 74 | </li> |
| 75 | +<li> |
| 76 | +<p>scheduling periodic jobs such as batch processes</p> |
| 77 | +</li> |
80 | 78 | </ul> |
81 | 79 | <h2>Task queue projects</h2> |
| 80 | +<p>The defacto standard Python task queue is Celery. The other task queue |
| 81 | +projects that arise tend to come from the perspective that Celery is overly |
| 82 | +complicated for simple use cases. My recommendation is to put the effort into |
| 83 | +Celery's reasonable learning curve as it is worth the time it takes to |
| 84 | +understand how to use the project.</p> |
82 | 85 | <ul> |
83 | 86 | <li> |
84 | 87 | <p>The <a href="http://www.celeryproject.org/">Celery</a> distributed task queue is the |
|
0 commit comments