简体   繁体   English

Python与CGI - 处理ajax

[英]Python with CGI - handle ajax

I'd like to: 我想:

  1. Send request from the browser to a python CGI script 从浏览器发送请求到python CGI脚本
  2. This request will start some very long operation 此请求将开始一些非常长的操作
  3. The browser should check progress of that operation every 3 seconds 浏览器应每3秒检查一次该操作的进度

How can I do that? 我怎样才能做到这一点?

You could do this - 你可以这样做 -

  1. Use Javascript to check progress every 3 seconds. 使用Javascript每3秒检查一次进度。
  2. Your Python script hit at anytime should give the progress at that instance. 您随时点击的Python脚本应该在该实例上提供进度。 It should essentially act as state-machine. 它本质上应该充当状态机。 You need a server here or maybe the progress is stored on some file. 您需要一台服务器,或者可能会将进度存储在某个文件中。

But having a server is the way to go... Maybe the long running process stores the progress in a MySQL table. 但拥有服务器是可行的方法......也许长时间运行的进程将进度存储在MySQL表中。

Hope this helps... 希望这可以帮助...

On the server side, you should probably look into some background queue / job runner of sorts, otherwise your request will be blocked once the long operation starts (depends on your server). 在服务器端,您可能应该查看一些后台队列/作业运行器,否则一旦长时间操作开始(取决于您的服务器),您的请求将被阻止。 I hear Celery is pretty good. 我听说芹菜很不错。 On the client side, do some polling every x seconds that checks the job status. 在客户端,每隔x秒进行一次轮询以检查作业状态。 If you're feeling adventurous, you could experiment with websockets, although they're not supported natively in all browsers (you could use 3rd party extensions to try and accomplish cross browser support). 如果你喜欢冒险的感觉,你可以使用WebSockets实验,虽然他们没有在所有浏览器(可以使用第三方扩展,试图实现跨浏览器支持), 原生支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM