与客户端更新异步鉴于长期运行的进程/人次 [英] Asynchronous view with client update for long running processes / views

查看:109
本文介绍了与客户端更新异步鉴于长期运行的进程/人次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序一个视图中,可以采取要求时相当长的时间才能提供给客户端。为了通知服务器运行的用户我想present用户某种就像一个进度条,但它不是一个真正的进度条,它更像是这样的:

I have one view in my app that can take quite a long time to be served to the client when requested. In order to inform the user that the server is working I want to present the user some kind like a progress bar, but it is not really a progress bar, it s more like this:

一)客户端请求/ MyApp的/ longrunningview /

a) client is requesting /myapp/longrunningview/

二)服务器返回的HTML侧的一部分,其中包括动画成才,看起来像服务器运行。在同一时间服务器计算响应的其余部分,这需要很长的

b) server returns part of the html side, including someting animated that looks like the server is working. At the same time server is calculating the rest of the response, the process which takes very long

三)服务器完成并返回到客户机的响应

c) server finished and returns the response to the client

d)在客户端的响应是presented

d) at client side the response is presented

这是不是一个真正的进度条,因为用户可以从一个页面请求该页面我的应用程序之外,我想立即返回一些东西给用户,而不需要等待完成的全过程。进度条的实现,我发现都是基于某种形式是发送到服务器,而客户端显示进度条,在等待答复。

It is not really a progress bar, since the user may request this page from a page outside my app, and I want to return instantly something to the user, without the need to wait for the entire process to be finished. The progress bar implementations that I have found are all based upon some form which is send to the server, while the client showing a progress bar, while waiting for the response.

推荐答案

我会用芹菜任务这样的事情。当用户发出请求,启动任务,然后返回一个页面的进度微调或什么的,然后要么有一块AJAX的点击服务器每隔几秒钟,询问是否做了,或利用socketio和Django的socketio或类似的包推已完成通知到客户端,这样就可以将您重定向到你需要的地方去。

I would use a celery task for this sort of thing. When the user makes the request, start the task and then send back a page with a progress spinner or whatever, then either have a piece of AJAX hit the server every few seconds asking if it's done, or utilize socketio and django-socketio or a similar package to push a "finished" notification to the client, which can then redirect you to wherever you need to go.

资源:

  • Django-SocketIO https://github.com/stephenmcd/django-socketio
  • SocketIO http://socket.io/
  • Celery http://celeryproject.org/
  • Django-Celery http://docs.celeryproject.org/en/latest/django/index.html

这篇关于与客户端更新异步鉴于长期运行的进程/人次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆