0

I will be the first to admit that I am still relatively new to Python. This board has been a lot of help...

I am working on a project where I am using a multi-threaded HTTP server and i need to send a signal back to the main loop. I am trying to base this off of this: Passing a Queue to ThreadedHTTPServer

Here is the extract of what I am currently doing (in Python 3.4):

if __name__ == '__main__':
    ...
    q1 = Queue(maxsize=0)

    ...
    remote_http_thread = threading.Thread(target=remote_threader, args=(q1))
    remote_http_thread.daemon = True
    remote_http_thread.start()
    ...


    while 1:
        time.sleep(1)
        try:
            my_message = q1.get_nowait()
            message_flag = 1
            print("GOT THE FOLLOWING QUEUE MESSAGE :", my_message)
        except:
            message flag = 0
        ...

The following is in a different file (my HTTP server code):

def remote_threader(qit):
    remote_Server = ThreadedHTTPServer(("", 35677), P_Server)
    remote_Server.qu = qit
    remote_Server.serve_forever()

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

class P_Server(BaseHTTPRequestHandler):
    def __init__(self, request, client_address, server):
    BaseHTTPRequestHandler.__init__(self, request, client_address, server)
    self.qu = server.qu # save the queue here.

    def go_GET(self):
        ...
        #  We have an event that we want to signal back to the main loop
        self.qu.put("Event")

The HTTP server works fine and I am now trying to pass the message back to the main loop so that certain events cause things to occur. running the above code results in an exception that P_Server has not attribute 'qu'

It appears from testing that any attribute I create in the init step (even a hard coded number or string) is not available in the "do_GET" section.

Can anyone let me know what i am doing wrong here and I can change it so that the main section will get the message put into the queue in the HTTP server class?

1 Answer 1

1

Try setting the attribute before the call to

BaseHTTPRequestHandler.__init__()

so the code becomes...

def remote_threader(qit):
    remote_Server = ThreadedHTTPServer(("", 35677), P_Server)
    remote_Server.qu = qit
    remote_Server.serve_forever()

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

class P_Server(BaseHTTPRequestHandler):
    def __init__(self, request, client_address, server):
        # THE ATTRIBUTE IS SET HERE
        self.qu = server.qu # save the queue here.
        # super's init called after setting attribute
        BaseHTTPRequestHandler.__init__(self, request, client_address, server)

    def go_GET(self):
        ...
        #  Accessed in GET
        self.qu.put("Event")

Worked for me when i set the attribute before the call to BaseHttpRequestHandler's init

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.