See Django Async Everyone's reaction should be celery this Message Queuing component, now with the most recommended is also this way. However, I demand this is the request to execute a small program, but can not be sure when the small program execution, response and timely, throw to queue processing of course, but for the sake of simplicity, decided to run a thread directly. (Of course, this is only an experiment, the application size is also very small, if the concurrency is high, there will be many problems)
This code was intercepted from view.py:
@login_required def search_area (request): prints = Printthread () Prints.start () return Retrieve ( Request, ' area ', ' areasearche.html ', [{' Name ': ' areaname ', ' mode ': ' contains '}] # #通过thread implement Django Import Threading Import Time class Printthread (threading. Thread): def run (self): print "Start .....%s"% (Self.getname (),) for I in range: time.sleep (1) Print i print "End .....%s"% (Self.getname (),)
From the results of the implementation of the requirements can be completed,
Start .... Thread-7 0 1 2 [24/oct/2014 15:09:30] "get/media/js/hawk.js http/1.1" 304 0 3 ... The end .... Thread-7
For timed-delay tasks, there are also aspects of high-concurrency asynchronous tasks that are used by MQ.
This article is from the "Orangleliu Notebook" blog, reproduced please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/41961295
Author Orangleliu using Attribution-NonCommercial-sharing protocol in the same way
Django increases asynchronous tasks with multithreading