简体   繁体   English

如何从运行在 ubuntu 上的 Apache 提供的 python 脚本生成一个长时间运行的 python 进程?

[英]How can I spawn a long running python process from a python script served by Apache running on ubuntu?

Everything I've found so far is good ONLY IF if it's not being used in a web environment.到目前为止,我发现的一切都是好的,前提是它没有在网络环境中使用。

I have a simple python 'admin tool' web app running in Apache, and I'm using the simple configuration in the vhost :我有一个在 Apache 中运行的简单 python“管理工具”Web 应用程序,并且我在 vhost 中使用了简单的配置:

AddHandler cgi-script .py.

There's no need for high performance, there's no python framework.不需要高性能,也没有 python 框架。 Users select certain criteria which is sent via a GET request to the server & a python script then gets called by apache and the report is compiled.用户选择某些条件,这些条件通过 GET 请求发送到服务器和 python 脚本,然后由 apache 调用并编译报告。 Problem is that the report complexity is growing, and the length of time it takes to compile this report can be > 20 minutes so I want to spawn a new totally independent process to generate the report asynchronously, allowing the server to return quickly to the client.问题是报告的复杂度越来越高,编译这个报告所需的时间可能超过 20 分钟,所以我想生成一个新的完全独立的进程来异步生成报告,允许服务器快速返回给客户端.

My issue is that I've tried absolutely everything I can find to be able to do this, and everything fails in the same way;我的问题是我已经尝试了所有我能找到的方法来做到这一点,但一切都以同样的方式失败; when I spawn a new process the parent script will not return to the client until the new process has completed.当我生成一个新进程时,父脚本在新进程完成之前不会返回到客户端。

All of these do exactly the same - they start a new process BUT the main script waits for the child process to finish before returning to the client.所有这些都完全相同 - 它们启动一个新进程,但主脚本在返回客户端之前等待子进程完成。 I want the main script to return straight back to the client, and leave the newly spawned process running to compile the report.我希望主脚本直接返回给客户端,并让新生成的进程继续运行以编译报告。

os.system("""
cd /the_directory
python -m web.wait &
""")
command = ['python', '/the_directory/wait.py &']
Popen(command, shell=True, start_new_session=True)
L = ['python', '/the_directory/wait.py']
os.spawnvp(os.P_NOWAIT, 'python', L)
L = ['test.sh']
os.spawnvpe(os.P_NOWAIT, '/the_directory/test.sh', L, os.environ)


where test.sh contains: 

#!/bin/sh
/the_directory/wait.py &
jobs = []
for i in range(5):
    p = multiprocessing.Process(target=wait.dostuff, )
    jobs.append(p)
    p.start()

I want the simplest possible solution to this - it's an admin tool maintained only by me for a very few users.我想要最简单的解决方案 - 它是一个管理工具,仅由我为极少数用户维护。

If there's really no way to accomplish this then I guess I will have to use cron running once a minute and use a simple queue, maybe with redis or something, but this is not ideal.如果真的没有办法做到这一点,那么我想我将不得不使用每分钟运行一次的 cron 并使用一个简单的队列,也许使用 redis 或其他东西,但这并不理想。

I used to do this kind of thing relatively easily in php, I can't believe it's not possible in python.我曾经在 php 中相对容易地做这种事情,我不敢相信在 python 中这是不可能的。

You could use Popen with shell=False您可以将Popenshell=False

example:例子:

Popen(["bash", "test.sh"], shell=False)

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

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