12👍
You spawn a separate thread and have it do the action.
t = threading.Thread(target=do_my_action, args=[my_argument])
# We want the program to wait on this thread before shutting down.
t.setDaemon(False)
t.start()
This will cause ‘do_my_action(my_argument)’ to be executed in a second thread which will keep working even after you send your Django response and terminate the initial thread. For example it could send an email without delaying the response.
5👍
If you have a long-running process, you have two simple choices.
-
Spawn a subprocess prior to sending the response page.
-
Create a “background service daemon” and pass work requests to it.
This is all outside Django. You use subprocess or some other IPC method to communicate with the other process.
- Django csrf token for Ajax
- Using bundle_files = 1 with py2exe is not working
- How to make a private download area with django?
- How to fix "InternalError: variable not found in subplan target list"
5👍
A common way to do this is to use message queues. You place a message on the queue, and worker threads (or processes, etc.) consume the queue and do the work after your view has completed.
Google App Engine has the task queue api http://code.google.com/appengine/docs/python/taskqueue/, amazon has the Simple Queue Service http://aws.amazon.com/sqs/.
A quick search didn’t turn up any django pluggables that look like accepted standards.
A quick and dirty way to emulate the functionality is to place the ‘message’ in a database table, and have a cron job periodically check the table to perform the work.
- Choosing and deploying a comet server
- Whats does assert _sre.MAGIC == MAGIC, SRE module mismatch AssertionError: SRE module mismatch error mean?
- Selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain while executing tests in Django with Selenium
- How can I test whether Django is running in debug mode?
- Where do I set environment variables for Django?
5👍
Django’s HttpResponse object accepts an iterator in its constructor:
http://docs.djangoproject.com/en/dev/ref/request-response/#passing-iterators
So you could do something like:
def myiter():
yield "my content"
enqueue_some_task()
return
def myview(request):
return HttpResponse(myiter())
The normal use of an iterator is to send large data without reading it all into memory. For example, read chunks from a file and yield appropriately. I’ve never used it in this way, but it seems like it should work.
- Django no csrftoken in cookie
- How do I get AWS credentials in the AWS ECS docker container?
- Opencv Live Stream from camera in Django Webpage
2👍
My favourite solution: A separate process that handles background tasks, typically things like indexing and sending notification mails etc. And then, during the view rendering, you send an event to the event handling system (I don’t know if Django has one built in, but you always need one anyway so you should have one) and the even system then puts a message in a message queue (which is trivial to write unless you have multiple machines or multiple background processes) that does the task in question.
- GeoDjango on Windows: Try setting GDAL_LIBRARY_PATH in your settings
- Django / postgres setup for database creation, for running tests
- Execute code on model creation in Django
- It is required that you pass in a value for the "algorithms" argument when calling decode()
- Django Admin + Filter Horizontal?
-1👍
In render to response, you pass the html page that you want displayed. That other page needs to send a post (via Javascript or something) that triggers the correct function in your views, then that view calls the correct next page to be shown.
- Django template {%for%} tag add li every 4th element
- Stop nosetests from printing logging information?
- Serving root-level static files on Heroku with Django?
- Reverse for 'account_email_verification_sent' not found. 'account_email_verification_sent' is not a valid view function or pattern name
-2👍
Perhaps I do not understand your question. But why not something simple like:
try:
return render_to_response()
finally:
do_what_needs_to_be_done()
- Django Custom User — Edit new CustomUser fields in admin
- How to cancel a delete in django signal
- Django Rest Swagger APIView