Tasks

django_futures.decorators.py

decorators.py – Simplifiy Asynchronous Code

class django_futures.decorators.ttask(*args, **kwargs)[source]

Bases: object

Run a task as a tornado callback. Great for async background code. If tornado is not running, then things are run synchronously.

Example: We define the task send_signup_confirmation() using the @ttask() decorator. When the task is called on line 21 the call will return imediately and the task will run at a later time after the view has finished.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from ttasks.decorators import ttask

@ttask()
def send_signup_confirmation(req, emsg):

    url = "https://api.myemailserver.example.com
    hc = HTTPClient()
    resp = hc.fetch(
        url,
        method='POST',
        body=tornado.escape.json_encode(emsg),
    )

    logger.debug("email result: %s", resp)


def a_view(request):
    # Process some stuff
    ...
    # Call the task
    send_signup_confirmation(request)

    # create and return a response
    ...
    return response
class django_futures.decorators.ctask(*args, **kwargs)[source]

Bases: object

Creates a ttask using a method/function that is also a Tornado coroutine.

This is a convenience decorator and is equivelant to decorting a function with @tornado.gen.coroutine and @ttask()

ctask will run the tornado.gen.coroutine on the decorated function first then decorate it with ttask.

For example, use this if you have a task that needs to make asynchronous http client calls.