简体   繁体   English

长时间运行python脚本

[英]long time running python script

I have application of following parts: 我有以下部分的应用:

client->nginx->uwsgi(python)

and some python scripts can be running long time (2-6 minutes). 一些python脚本可以运行很长时间(2-6分钟)。 After execution of script I should give to client content, but connection break with error "gateway timeout 504". 执行脚本后,我应该给客户端内容,但连接中断错误“网关超时504”。 What can I use for my case to avoid this error? 我可以用什么来避免这个错误?

So is your goal to reduce the run time of the scripts, or to not have them time out? 那么你的目标是减少脚本的运行时间,还是不让它们超时? Browsers are going to give up on a 6 minute request no matter what you try. 无论你尝试什么,浏览器都会放弃6分钟的请求。

Perhaps try doing the work on the server, and then polling for progress with AJAX requests? 也许尝试在服务器上进行工作,然后使用AJAX请求轮询进度?

Or, if possible, try optimizing the scripts. 或者,如果可能,请尝试优化脚本。 For example, if you have some horribly slow SQL stuff going on, try cleaning that up. 例如,如果你有一些非常缓慢的SQL内容,请尝试清理它。

Otherwise, without more information, a more specific answer is hard to give. 否则,如果没有更多信息,很难给出更具体的答案。

I once set up a system where the "main page" contained an Iframe which showed the output of the long running program as text/plain. 我曾经建立了一个系统,其中“主页”包含一个Iframe,它显示长时间运行程序的输出为text / plain。 I think the the handler for the the Iframe content was a Python CGI script which emitted all headers and then the program output line by line under an Apache server. 我认为Iframe内容的处理程序是一个Python CGI脚本,它发出所有头文件,然后在Apache服务器下逐行发出程序输出。

I don't know whether this would work under your configuration. 我不知道这是否适用于您的配置。

这在很大程度上取决于您的服务器设置(即将数据推送回客户端是多么容易),但是在运行冗长的应用程序时可以定期发送一些“空”内容(例如,假设输出为html的简单换行符)浏览器认为这只是一个缓慢的连接,而不是一个停滞的连接?

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

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