Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将队列传递给ThreadedHTTPServer_Python_Multithreading_Http_Queue - Fatal编程技术网

Python 将队列传递给ThreadedHTTPServer

Python 将队列传递给ThreadedHTTPServer,python,multithreading,http,queue,Python,Multithreading,Http,Queue,我想将队列对象传递给基本ThreadedHTTPServer实现。我现有的代码工作得很好,但我希望有一种安全的方式向HTTP请求发送调用。通常这可能由web框架处理,但这是一个硬件有限的环境 我的主要困惑在于如何传递队列(或任何)对象以允许访问环境中的其他模块 我当前正在运行的基本代码模板: import base64,threading,urlparse,urllib2,os,re,cgi,sys,time import Queue class DemoHttpHandler(BaseHTT

我想将队列对象传递给基本ThreadedHTTPServer实现。我现有的代码工作得很好,但我希望有一种安全的方式向HTTP请求发送调用。通常这可能由web框架处理,但这是一个硬件有限的环境

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

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

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...'

您的代码未运行,但我对其进行了修改,使其能够运行:

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...'

谢谢你,这看起来和我想做的一样。我在理解如何将参数传递到这个构造中时遇到了困难。对这段代码的解释会有所帮助。我不确定这个代码中的队列在做什么;我遇到了这个问题,使用了一个全局变量。我需要回去应用这个模式。不错。