将队列传递给ThreadedHTTPServer [英] Passing a Queue to ThreadedHTTPServer

查看:439
本文介绍了将队列传递给ThreadedHTTPServer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Queue对象传递给基本的ThreadedHTTPServer实现。我现有的代码工作得很好,但我想要一种安全的方式来发送和来自我的HTTP请求。通常这可能是由Web框架处理的,但这是一个硬件限制的环境。

I would like to pass a Queue object to a base ThreadedHTTPServer implementation. My existing code works just fine, but I would like a safe way to send calls to and from my HTTP requests. Normally this would probably be handled by a web framework, but this is a HW limited environment.

我的主要困惑在于如何将队列(或任何)对象传递给允许访问我环境中的其他模块。

My main confusion comes on how to pass the Queue (or any) object to allow access to other modules in my environment.

我目前正在运行的基本代码模板:

The base code template I have currently running:

import base64,threading,urlparse,urllib2,os,re,cgi,sys,time
import Queue

class DemoHttpHandler(BaseHTTPRequestHandler):       
    def __init__(self, request, client_address, server,qu):
        BaseHTTPRequestHandler.__init__(self, request, client_address, server)
    def do_GET(self):
        ...
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""

def main():
    listen_interface = "localhost"
    listen_port = 2323  
    server = startLocalServer.ThreadedHTTPServer((listen_interface, listen_port), startLocalServer.DemoHttpHandler)
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    print 'started httpserver thread...'


推荐答案

你的代码没有运行但是我修改它所以你可以让它运行:

Your code does not run but I modify it so zou can make it run:

import base64,threading,urlparse,urllib2,os,re,cgi,sys,time
import Queue

class DemoHttpHandler(BaseHTTPRequestHandler):       
    def __init__(self, request, client_address, server):
        BaseHTTPRequestHandler.__init__(self, request, client_address, server)
        self.qu = server.qu # save the queue here.
    def do_GET(self):
        ...
        self.qu # access the queue self.server.qu
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""

def main():
    listen_interface = "localhost"
    listen_port = 2323  
    qu = Queue.Queue()
    server = startLocalServer.ThreadedHTTPServer((listen_interface, listen_port), startLocalServer.DemoHttpHandler)
    server.qu = qu # store the queue in the server
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    print 'started httpserver thread...'

这篇关于将队列传递给ThreadedHTTPServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆