使用django中的celery和ajax动态更新网页

时间:2022-02-21 19:18:22

I need to display some analytics to the user; however, occasionally these analytics can take a while to crunch (sometimes 2-5 seconds). Instead of waiting on these results, I think I would like to have them updated in a dynamic way on the webpage.

我需要向用户显示一些分析;然而,偶尔这些分析可能需要一段时间来处理(有时2-5秒)。我不想等待这些结果,而是希望在网页上以动态的方式更新它们。

I already have celery implemented in a limited way, but what I would like to do is send some tasks to celery, have the screen render, and once the function is complete, send the return value to the webpage to by udpated.

我已经以有限的方式实施了芹菜,但我想做的是将一些任务发送到芹菜,屏幕渲染,一旦功能完成,将返回值发送到网页到udpated。

Once the task is created, should I monitor its status, or once its complete can I just send the json to the webpage directly? some code examples would be helpful.

创建任务后,我应该监控其状态,还是一旦完成,我可以直接将json发送到网页吗?一些代码示例会有所帮助。

Thank you.

1 个解决方案

#1


1  

"Pushing" data is very hard to do with django. It is not supported natively and the solutions are hacky at best.

使用django很难做到“推送”数据。它本身不受支持,解决方案充其量只是hacky。

There are simpler options, the most popular being ajax polling.

有更简单的选项,最流行的是ajax轮询。

  1. the user could set off a request to do analytics.

    用户可以设置执行分析的请求。

  2. django queues the process and marks it as processing in storage

    django将进程排队并将其标记为存储中的处理

  3. every x seconds the user make an ajax call requesting the status of the request.

    每隔x秒,用户发出一个请求请求状态的ajax调用。

  4. when the request is finished the storage is updated to reflect this the next poll request made can retrieve the appropriate data.

    当请求完成时,存储被更新以反映这一点,下一个轮询请求可以检索适当的数据。

HTML5 has native support for socket based communication. but django does not support it.

HTML5本机支持基于套接字的通信。但是django不支持它。

Django Push HTTP Response to users

Django向用户推送HTTP响应

#1


1  

"Pushing" data is very hard to do with django. It is not supported natively and the solutions are hacky at best.

使用django很难做到“推送”数据。它本身不受支持,解决方案充其量只是hacky。

There are simpler options, the most popular being ajax polling.

有更简单的选项,最流行的是ajax轮询。

  1. the user could set off a request to do analytics.

    用户可以设置执行分析的请求。

  2. django queues the process and marks it as processing in storage

    django将进程排队并将其标记为存储中的处理

  3. every x seconds the user make an ajax call requesting the status of the request.

    每隔x秒,用户发出一个请求请求状态的ajax调用。

  4. when the request is finished the storage is updated to reflect this the next poll request made can retrieve the appropriate data.

    当请求完成时,存储被更新以反映这一点,下一个轮询请求可以检索适当的数据。

HTML5 has native support for socket based communication. but django does not support it.

HTML5本机支持基于套接字的通信。但是django不支持它。

Django Push HTTP Response to users

Django向用户推送HTTP响应