守护python的BaseHTTPServer

守护python的BaseHTTPServer

本文介绍了守护python的BaseHTTPServer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理需要嵌入HTTP服务器的守护程序。我正在尝试使用BaseHTTPServer进行操作,当我在前台运行它时,它可以正常工作,但是当我尝试将守护程序派生到后台时,它将停止工作。我的主要应用程序继续工作,但是BaseHTTPServer无效。

I am working on a daemon where I need to embed a HTTP server. I am attempting to do it with BaseHTTPServer, which when I run it in the foreground, it works fine, but when I try and fork the daemon into the background, it stops working. My main application continues to work, but BaseHTTPServer does not.

我相信这与BaseHTTPServer将日志数据发送到STDOUT和STDERR有关。我将那些重定向到文件。以下是代码段:

I believe this has something to do with the fact that BaseHTTPServer sends log data to STDOUT and STDERR. I am redirecting those to files. Here is the code snippet:

# Start the HTTP Server
server = HTTPServer((config['HTTPServer']['listen'],config['HTTPServer']['port']),HTTPHandler)

# Fork our process to detach if not told to stay in foreground
if options.foreground is False:
    try:
        pid = os.fork()
        if pid > 0:
            logging.info('Parent process ending.')
            sys.exit(0)
    except OSError, e:
        sys.stderr.write("Could not fork: %d (%s)\n" % (e.errno, e.strerror))
        sys.exit(1)

    # Second fork to put into daemon mode
    try:
        pid = os.fork()
        if pid > 0:
            # exit from second parent, print eventual PID before
            print 'Daemon has started - PID # %d.' % pid
            logging.info('Child forked as PID # %d' % pid)
            sys.exit(0)
    except OSError, e:
        sys.stderr.write("Could not fork: %d (%s)\n" % (e.errno, e.strerror))
        sys.exit(1)


    logging.debug('After child fork')

    # Detach from parent environment
    os.chdir('/')
    os.setsid()
    os.umask(0)

    # Close stdin
    sys.stdin.close()

    # Redirect stdout, stderr
    sys.stdout = open('http_access.log', 'w')
    sys.stderr = open('http_errors.log', 'w')

# Main Thread Object for Stats
threads = []

logging.debug('Kicking off threads')

while ...
  lots of code here
...

server.serve_forever()

我在做什么w错误在这里还是以某种方式阻止了BaseHTTPServer的守护进程?

Am I doing something wrong here or is BaseHTTPServer somehow prevented from becoming daemonized?

编辑:更新了代码,以演示以前缺少的其他代码流,并且log.debug在派生的后台守护程序中显示,我在派生后点击代码。

Updated code to demonstrate the additional, previously missing code flow and that log.debug shows in my forked, background daemon I am hitting code after fork.

推荐答案

经过一番谷歌搜索后,我,此后我得到了:

After a bit of googling I finally stumbled over this BaseHTTPServer documentation and after that I ended up with:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  """Handle requests in a separate thread."""

server = ThreadedHTTPServer((config['HTTPServer']['listen'],config['HTTPServer']['port']), HTTPHandler)
server.serve_forever()

大多数情况是在我分叉后最终解决了我的问题。

Which for the most part comes after I fork and ended up resolving my problem.

这篇关于守护python的BaseHTTPServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:17