django project avoid reload page where work algorithm

38 views
Skip to first unread message

Xristos Xristoou

unread,
Oct 8, 2017, 6:36:25 AM10/8/17
to Django users

hello

i need some help
I have a website where the user can be put three numbers on my html template and get some results from my personal mathematical algorithm. the result save at user personal table on my database and can see in specific tab in my website.

my problem is where the algorithm to calculate result maybe take time between 5-10 minutes in this time the browser stay on reload. if user change tab or close browser or maybe have problem with internet connection then loose that request and need again to put the numbers and again wait for results.

I want after the user request from my form in html to keep this request and work my algorithm without reload the page and where the algorithm finish then to send the user some email or message or just need the user visit the tab of results to see new results.

that I want to avoid the problems where the algorithm is in running and user loose data or request or time.

is easy to do that using suproccess,celery or RabbitMQ ?

any idea ?

here the code


views.py

def math_alg(request):
    if request.method == "POST":
        test = request.POST.get('no1')
        test = request.POST.get('no3')
        test = request.POST.get('no3')
        #start algorith
        calc_math(no1,no1,no3,result)
        instance = rmodel.objects.create(user=request.user,rfield=result)
        instance.save
    return render(request, 'page.html', {'result': result})

html :

<form action="" method="POST">{% csrf_token %}
  op math calculate:<br>
  <input type="number" name="no1" step="any" min="0" max="1" value="0.5">
  <input type="number" name="no2" step="any" min="0" max="1" value="9999">
  <input type="number" name="no3" step="any" min="0" max="1" value="1000000000000000">
<br>
  <input class="btn btn-primary" type="submit">
{{result }}
</form>

Jani Tiainen

unread,
Oct 8, 2017, 9:09:37 AM10/8/17
to django...@googlegroups.com
Hi.

For long running operatios celery tasks are pretty good choice. Celery can use rabbitmq as messaging queue so those two aren't mutually exclusive.

Once you have task running it is possible to use for example ajax polling to query status of the task and once complete inform end user that task is complete.

Or you can send email at the end of the task.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/86fc72c5-6dcc-4734-8f1d-1e187b1d93f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Xristos Xristoou

unread,
Oct 8, 2017, 11:58:49 AM10/8/17
to Django users
how first to can avoid long reload ?

Jani Tiainen

unread,
Oct 8, 2017, 2:13:16 PM10/8/17
to django...@googlegroups.com
For example with celery you create celery task that does the actual math. Creation of task is fast operation. Once task is created request returns.

In the background celery worker picks your task and executes it. It doesn't matter how long that takes. Once task is completed worker waits for next task.

Every task gets id which you can return to frontend and then for example using ajax call you could query if task is ready and inform user about progress or completition.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Xristos Xristoou

unread,
Oct 8, 2017, 2:24:51 PM10/8/17
to Django users




In the background celery worker picks your task and executes it. It doesn't matter how long that takes. Once task is completed worker waits for next task.

Every task gets id which you can return to frontend and then for example using ajax call you could query if task is ready and inform user about progress or completition.


can you show some example with my code ?is easy to bulid ? 

James Schneider

unread,
Oct 9, 2017, 4:05:27 AM10/9/17
to django...@googlegroups.com
It is not necessarily trivial to implement Celery or any sort of batch processor. It requires a separate server demon that may not be supported on your hosting platform. I'd suggest combing through the docs for Celery and the specific Django documentation for it, which includes many examples.


-James
Reply all
Reply to author
Forward
0 new messages