DjangoFutures¶
Contents:
Install¶
The current version of Django Futures is a development build!
Django Futures is still a proof of concept. That said, please install it, try it, and give feedback.
There is package available and is the easiest way to install Django
Futures. Simply install with pip
as usual.
pip install djang-futures
If you prefer the bleeding edge, install using the sources from GitHub.
pip
can also be used to install from source.
- Clone the repo:
git clone https://github.com/jeffbuttars/django-futures
pip
the package sources:pip install django-futures/src
If you don’t want to use pip
, you can run the setup script directly.
Clone the directory as in step one above then:
- Change to the package source directory:
cd django-futures/src
- Run the
setup.py
script:python ./setup.py install
Examples¶
Here is a simple asynchronous view that fetches a web page.
from tornado import gen
from django_futures.http_client import HttpClient
from django.views.generic import TemplateView
from core.views import BaseTemplateView
class TestAsyncHttpClient(BaseTemplateView):
template_name = "test_async_httpclient.html"
num_client_options = (1, 5, 10, 25, 50, 100)
@gen.coroutine
def get(self, request):
"""
Here we make an asynchrounous web call using the asynchrounous
aware web client available with Django Futures
"""
# Go and grab a web page asynchronously
http_client = HttpClient()
res = yield http_client.get('http://yahoo.com')
ctx = {
'web_response': res
}
# Build a Django response
myres = super(TestAsyncHttpClient, self).get(request, **ctx)
# In an asynchronous view, we must render a Django response using
# the render() method of the request object.
request.render(myres)
# get()
# TestAsyncHttpClient
run_tornado¶
HTTP Client¶
django_futures.http_client.py
http_client.py
– Asynchronous HTTP Client¶
This is a ‘smart’ HTTPClient wrapper that also makes requests a bit simpler by borrowing conventions from the ‘requests’ module.
If a tornado IOLoop is running, then async requests are made.
Otherwise, tornado’s run_sync()
method is used to make the
calls synchronous.
-
class
django_futures.http_client.
HttpClient
(*args, **kwargs)[source]¶ Bases:
object
Docstring for HttpClient
-
get
(url, params=None, **kwargs)[source]¶ GET wrapper. Always returns a future.
Parameters: - url (type description) – arg description
- kwargs (type description) – arg description
Returns: Return type:
-
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 withttask
.For example, use this if you have a task that needs to make asynchronous http client calls.
Sources¶
Github¶
Django Futures is an open source project hosted on GitHub. Browse, download and fork the sources at the Django Futures GitHub repository