3

I have a problem with django, it was like, when users submit some data,it will go into view.py to process, and it will eventually get to the success page.

But the process is too long.I don't want the users to wait for that long time.What i want is to get to the success page right away after users submiting the data.And the Server will process the data after return the success page.

Can you please tell me how to deal with it? that was my code,but i don't know why it didn't work.

url.py

from django.conf.urls import patterns, url
from hebeu.views import handleRequest

urlpatterns = patterns('',
    url(r'^$', handleRequest),
)

view.py

def handleRequest(request):
    if request.method == 'POST':
        response = HttpResponse(parserMsg(request))
        return response
    else:
        return None

def parserMsg(request):
    rawStr = smart_str(request.body)
    msg = paraseMsgXml(ET.fromstring(rawStr))
    queryStr = msg.get('Content')
    openID = msg.get('FromUserName')
    arr = smart_unicode(queryStr).split(' ')
    #start a new thread
    cache_classroom(openID,arr[1],arr[2],arr[3],arr[4]).start()

    return "success"

My English is not good, i hope you can understand.

Nijin Narayanan
  • 2,269
  • 2
  • 27
  • 46
user3354327
  • 31
  • 1
  • 3
  • I met the same condition before, just use ajax! see here http://stackoverflow.com/questions/18426456/django-ajax-no-refreshdjango-view-without-redirecting-or-refreshing-a-page/21846363#21846363 – WeizhongTu Feb 26 '14 at 08:34

3 Answers3

1

Take a look at Celery, it is a distributed task queue that will handle your situation perfectly. There is a little bit of setup to get everything working but once that is out of the way Celery really easy to work with.

For integration with Django start here: http://docs.celeryproject.org/en/latest/django/index.html

Tim
  • 2,510
  • 1
  • 22
  • 26
1

Write a management command to for parseMsg and trigger that using subprocess.popen and return success to user and parseMsg process will run in background. if this kind of operations are more in the application then you should use celery.

loki
  • 387
  • 2
  • 8
1

This is quiet easy, encapsulate #start a new thread with the code below

from threading import Thread
from datetime import datetime

class ProcessThread(Thread):
    def __init__(self, name):
        Thread.__init__(self)
        self.name = name
        self.started = datetime.now()

    def run(self):
        cache_classroom(openID,arr[1],arr[2],arr[3],arr[4]).start()
        # I added this so you might know how long the process lasted
        # just incase any optimization of your code is needed
        finished = datetime.now()
        duration = (self.start - finished).seconds
        print "%s thread started at %s and finished at %s in "\
              "%s seconds" % (self.name, self.started, finished, duration)

# let us now run start the thread
my_thread = ProcessThread("CacheClassroom")
my_thread.start()
  • how will you ensure the thread is terminated properly when app shuts down ? All resources grabbed by the forked thread must be relesed in a graceful way. One solution to the problem is to use shutdown hooks by using signals but it has own set of problems (one is , you can't set signal handler from a non main thread) – Shailesh Pratapwar Jan 06 '18 at 12:32