Django and asynchronous tasks

150 views
Skip to first unread message

Alain Muls

unread,
Nov 10, 2016, 3:05:29 AM11/10/16
to django...@googlegroups.com
Hi All

I am building a website which makes calculations about the visibility of
satellites. These calculations take about half a minute so I do not want
to block the site during this time. I found django-rq and was able to
start a asynchronous task which handles the calculations.

The problem I have is how do I find out when the calculations of the
task thread are done so that I can direct the results to another web
page which will display them?

Thanks for your help

Alain Muls


Daniel Chimeno

unread,
Nov 10, 2016, 3:35:06 AM11/10/16
to Django users
Hello,
One simple approach is to persist the calculations in the database, then you can use a view to display them in another page.

Hope it helps.

Antonis Christofides

unread,
Nov 10, 2016, 3:55:08 AM11/10/16
to django...@googlegroups.com
(Note: The most popular way to do asynchronous tasks is celery, but indeed some
people prefer django-rq, which is said to be simpler. But your question is not
affected by that.)

I'm not an expert but I think that the "correct" way to do what you want would
be to use comet (i.e. the opposite of ajax). However, if the work required to
make that work is not justified by the budget or the business case, you might be
able to get away with a message like "This information is being (re)calculated.
Reload the page after half a minute to view the updated results." (That's what I
did last time :-)

Regards,

Antonis

http://djangodeployment.com

Alain Muls

unread,
Nov 10, 2016, 4:54:14 AM11/10/16
to Django users
Hi 

Tx for the suggestion but how do I reload a page after eg 30 seconds? 
Is there no mechanism that when the background tasks finishes to have a web page called which could display the results?
I had a look at the signal mechanism of Django but I think that is not working since the background task is in another environment than the django apps which called it. 

bye/alain

Alain Muls

unread,
Nov 10, 2016, 4:55:06 AM11/10/16
to Django users
Hi

What does 'persist the solution in a database' means and how can I react on it?

bye/alain

Antonis Christofides

unread,
Nov 10, 2016, 5:39:20 AM11/10/16
to django...@googlegroups.com

Is there no mechanism that when the background tasks finishes to have a web page called which could display the results?

Web pages cannot be "called". They are loaded by the browser. So, what you want is a mechanism that notifies the browser that an event has occurred in the server. That mechanism is comet.

Antonis Christofides
http://djangodeployment.com
--
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...@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/6ae68ef3-6a52-4a59-a87b-3fcd627b4f26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ludovic coues

unread,
Nov 10, 2016, 5:55:01 AM11/10/16
to django...@googlegroups.com
Websocket provide a way for server to send information to the client
without waiting for input from the client.

Django channels [1] is a project to bring native support of websocket
to django. There are alternatives which might involve a bit more of
work

[1] https://channels.readthedocs.io/en/stable/
> https://groups.google.com/d/msgid/django-users/0dcd7af0-6833-8174-363a-a68c5b3ff5c9%40djangodeployment.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Alain Muls

unread,
Nov 30, 2016, 2:44:14 AM11/30/16
to Django users
Hi All

I spent several days reading and searching examples of how to implement channels. The current status of my project is as follows:
  • I have a page which lets the user enter the data used for the offline calculations, a POST request is send
  • the post request is handled and I can forward these data to a rq-worker, after which I open a basic web-page showing the input data. This page should also hold the results of the asynchronous calculations
  • the rq-worker starts the requested download of data from a site and I store the data in the apps (gnsspredict) directory. The rq-worker stops with a success reported in the log.
  • not yet implemented, but this is similar to the download thread, I shall further launch 2 to 3 workers to split up the work so that I can inform the user of the progress of the async calculations
But this is where I do not know what to do next.

As far as I understand 
  • the rq-worker can send its results using a dictionary to a 'reply-channel', but where does this channel goes to? 
  • of the few complete working examples on github I found I assume that I have to write a javascript (which I never used) to treat the results from the worker thread? I also remark that the javascript has as name 'function()', is this always the case? If so, for all 3 to 4 workers how do I write a script that takes action on which asyn cthread that has finished?
All help is appreciated

Thanks
Alain

Andrew Godwin

unread,
Nov 30, 2016, 6:39:01 PM11/30/16
to django...@googlegroups.com
Hi Alain,

I'm not quite sure what you're trying to accomplish by mixing rq and channels; Channels just allows you to either write things against WebSocket connections, or to build your own eventing system from scratch on the same base code.

If you're trying to return results to a webpage, you'll need the reply_channel name that points to an open socket connected to that page, or have connected the socket to a group when it connected and send to that group, and write your own javascript on the client end to handle it.

Andrew

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.

Karim

unread,
Aug 30, 2018, 7:39:58 AM8/30/18
to Django Users
On Thu, Nov 10, 2016 at 7:05 PM Alain Muls <alain...@gmail.com> wrote:
Hi All
[...]

I found celery very hard to work with. I liked a lot `https://django-q.readthedocs.io/`. Give it a go.

--
Karim N. Gorjux
Reply all
Reply to author
Forward
0 new messages