【问题标题】:python CGI call new subprocess without waitingpython CGI无需等待即可调用新的子进程
【发布时间】:2018-08-05 16:46:25
【问题描述】:

我在这里阅读了关于在 CGI 调用的脚本中启动另一个 python 脚本的问题的各种帖子,即,我有一个在启用 CGI 的 Raspberrypi 上运行的 apache2 服务器。

1) 从浏览器:webform 数据被发送到 CGI 接口并调用我的 python 脚本test1.py

2)来自test1.py 我想打电话给test2.py(它将永远运行或在任何需要的时间运行(它会在某处不断更新值)

3) 重点是:我希望 CGI 脚本立即显示一些结果让我们在“结果网页”上说“test2 已启动”并在后台运行我的test2.py

但是发生的事情是带有表单的网页永远等待我的 test2.py 完成!

我尝试过类似的方法:

p = subprocess.Popen([sys.executable, '/var/www/cgi-bin/test2.py'],
                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

但这不起作用(即等待脚本 test2 完成...)

有人可以帮忙吗?

【问题讨论】:

    标签: python subprocess cgi


    【解决方案1】:


    你的第一个问题的答案是这个链接。getting start Cgi programmin

    第二个问题的答案:您应该使用多线程编程。
    如果您在模块 test2 中有示例函数 ...

    #in test1.py
    import threading
    import test2
    T1 = threading.Thread(test2.sample(object))
    T1.start()
    

    回答你的第三个问题:
    然后,您可以在您的站点路径中创建一个文件夹来存放您的 cgi 文件,或者配置某些目录以将某些文件类型作为 cgi 脚本处理。 这在上面的 apache2 文档中有很好的描述,但本质上你要让 cgi 文件夹中的所有文件都被执行,你会使用这个 conf:

    选项 ExecCGI SetHandler cgi脚本

    并允许 .py 文件作为脚本在特定文件夹中执行,您将使用此 conf:

    <Directory /srv/www/yoursite/public_html>
        Options +ExecCGI
        AddHandler cgi-script .py
    </Directory>
    

    一旦你有了它,如果你正在运行 python 3,你可以制作一个像这样的 python 脚本,并将它粘贴到为 cgi 配置的任何文件夹中:

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-# enable debugging
    import cgitb
    cgitb.enable()    print("Content-Type: text/html;charset=utf-8")
    print()    print("Hello World!")
    

    【讨论】:

    • 感谢您的出色回答!事实上,我已经设置了 CGI 等它正在工作(例如,使用短脚本就可以了)我的问题是从 CGI 启动一个长脚本,然后不要等待而是立即显示一个网页
    【解决方案2】:

    感谢这是一个很好的答案!

    我刚刚将它添加到我的脚本 test1.py 中(atm 在 test2.py 中没有任何功能,只是原始代码):

    [...] 导入测试2 cgitb.enable()

    T1 = threading.Thread(test2) T1.start()

    [...]

    尽管我注意到 test2 确实是由线程启动的,但网页仍然永远等待... 你知道如何让网页显示一些东西而不是等待吗?

    请注意,我也尝试过:

    pid = os.fork()
    #if pid==0 then we are in the child process
    if(pid==0):
        T1 = threading.Thread(test2)
        T1.start()
        #i would hope that here the child process is detached from making wait the CGI!! but no
    else:
        print('Content-type: text/html\n\n')
        print('<h1>Python Script Test</h1>')
        form = cgi.FieldStorage()
        tempThVal = form['tempThresh'].value
        print('Temperature threshold= ' + tempThVal)
    

    但该死的浏览器仍在等待它完成!

    【讨论】:

      【解决方案3】:

      要解决这个问题,您可以将自己的解决方案与我的答案结合起来得到正确的答案。

      #in test1.py
      
      class my_thread(threading.Thread):
          def __init__(self):
              super().__init__()
      
          def run(self):
              p = subprocess.Popen([sys.executable, '/var/www/cgi-bin/test2.py'],
                       stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
              ########################
              #or your test2 statment#
              ########################
      pid = os.fork()
      #if pid==0 then we are in the child process
      if(pid==0):
          T1 = my_thread()
          T1.start()
      #i would hope that here the child process is detached from making wait the CGI!! but 
      no
      else:
         print('Content-type: text/html\n\n')
         print('<h1>Python Script Test</h1>')
         form = cgi.FieldStorage()
         tempThVal = form['tempThresh'].value
         print('Temperature threshold= ' + tempThVal)
      

      我在Django框架中使用这种方法解决了这个问题

      【讨论】:

      • 谢谢!这很有趣,我今天要试试。非常感谢您的快速解答。
      • 刚刚测试过......仍然是同样的问题(页面一直在等待 test2 的结束,但确实像往常一样开始 test2):'(((真的令人沮丧!!!还有其他想法吗?也许cron 的东西?
      【解决方案4】:

      让我发布我最新的代码(以防你发现什么):

      #!/usr/bin/python3
      
      import cgi
      import cgitb
      import sys
      import subprocess
      import os
      import threading
      import test2
      cgitb.enable()
      
      class my_thread(threading.Thread):
          def __init__(self):
              super().__init__()
      
          def run(self):
              p = subprocess.Popen([sys.executable, '/var/www/cgi-bin/test2.py'],
                       stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
              ########################
              #or your test2 statment#
              ########################
      pid = os.fork()
      #if pid==0 then we are in the child process
      if(pid==0):
          pid = os.fork()
          if(pid==0):
              T1 = my_thread()
              T1.start()
      else:
         print('Content-type: text/html\n\n')
         print('<h1>Python Script Test</h1>')
         form = cgi.FieldStorage()
         tempThVal = form['tempThresh'].value
         print('Temperature threshold= ' + tempThVal)
      

      【讨论】:

      • 我想知道我从这个 CGI 中的表单接收数据这一事实是否会导致一些问题(虽然我无法想象为什么?)
      猜你喜欢
      • 2014-11-04
      • 1970-01-01
      • 2021-02-13
      • 2012-07-12
      • 1970-01-01
      • 2021-11-25
      • 2020-04-18
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多