Skip to content

Handle Request Queue

Jana Rajakumar edited this page Feb 27, 2018 · 5 revisions

The API all allows to perform requests in the background with a Queuing system. The following settings are used to customise this behaviour.

QUEUE_POST_ENABLED = False # If enabled, all POST requests will be served by a Queueing system with 202 Immediate Response.
QUEUE_PUT_ENABLED = False # If enabled, all PUT requests will be served by a Queueing system with 202 Immediate Response.
QUEUE_DELETE_ENABLED = False # If enabled, all DELETE requests will be served by a Queueing system with 202 Immediate Response.
QUEUE_RUNNER = 'THREAD' # The method for processing background taks in Queue system. Choices are: "PROCESS" or "THREAD" or "CELERY"
BROKER_URL = 'redis://localhost:6379' # Config only if QUEUE_RUNNER is set to "CELERY". RabbitMQ, Redis or any compatible other broker.
  • Available methods for Queue

  • THREAD: Threading is a technique for decoupling tasks which are not sequentially dependent. Threads can be used to improve the responsiveness of applications that accept user input while other tasks run in the background. A related use case is running I/O in parallel with computations in another thread. Read more: https://docs.python.org/2/tutorial/stdlib2.html#multi-threading

  • PROCESS: Multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows. Read more: https://docs.python.org/2/library/multiprocessing.html

  • CELERY: Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well. Read more about celery with django at http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html. If CELERY is being used, the config for BROKER_URL must also be specified as mentioned in http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-broker_url.

  • Difference between Multi-processing vs Multi-threading

  • The threading module uses threads, the multiprocessing module uses processes. The difference is that threads run in the same memory space, while processes have separate memory. This makes it a bit harder to share objects between processes with multiprocessing. Since threads use the same memory, precautions have to be taken or two threads will write to the same memory at the same time. This is what the global interpreter lock is for. Spawning processes is a bit slower than spawning threads. Once they are running, there is not much difference. Referenced from, https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python

If queuing is enabled for any method (POST, PUT or DELETE), the client will receive an immediate 202 Request Accepted response from the API as follows.

{
    "status": "http://localhost:8000/queue/5a8da2763b0eb706ca4e100c",
    "message": "Request Accepted"
}

While the request is still pending, any GET request to the status url will result in a 200 response with the partial Activity of that request.

{
    "id": "http://localhost:8000/queue/5a8da3333b0eb706ca4e1028",
    "status": "Pending",
    "activity": {
        "username": "staff",
        "requestPath": "/book1/manifest",
        "requestMethod": "POST",
        "remoteAddress": "127.0.0.1",
        "startTime": "2018-02-21T11:49:55.610000"
    }
}

Once the background task is completed, any further GET requests to the the status url will result in a 200 response that retrieves the Activity object of that request.

{
    "id": "http://localhost:8000/activity/5a8da2763b0eb706ca4e100d",
    "username": "staff",
    "requestPath": "/book1/manifest",
    "requestMethod": "POST",
    "remoteAddress": "127.0.0.1",
    "responseCode": 201,
    "startTime": "2018-02-21T11:46:46.175000",
    "endTime": "2018-02-21T11:46:46.900000",
    "queueID": "5a8da2763b0eb706ca4e100c",
    "responseBody": {
        "@id": "http://localhost:8000/book1/manifest",
        "@type": "sc:Manifest"
    },
    "requestBody": { }
}
Clone this wiki locally