problems with threaded server.

Eric Wagner pezking at uclink4.berkeley.edu
Tue May 15 16:31:08 EDT 2001


Hello.

I am sorry if this question has come up before, but...

why doesn't this work as expected?

#!/usr/bin/env python

import SocketServer
import sys
import time

class testServer(SocketServer.ThreadingTCPServer):
    def __init__(self, server_address, RequestHandlerClass):
        SocketServer.ThreadingTCPServer.__init__(self, server_address,
            RequestHandlerClass)

class testHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        print 'got something'
        print self.request

def main(port):
    testServer(('127.0.0.1', port), testHandler).serve_forever()

if __name__ == '__main__':
    main(int(sys.argv[1]))
#    time.wait(10)


When run, I get the error that there is a bad file descriptor when the
StreamRequestHandler tries to create read and write files from the
request
socket.  It seems that when the request socket is passed into the new
thread,
the socket's fd changes to -1.  I'm not at all experienced writing
threaded
programs, so I'm not sure if this is standard behavior or not.

well, anyways, when I add this method to the testServer class:

    def get_request(self):
        l = list(self.socket.accept())
        l[0] = l[0].dup()
        return tuple(l)

it works.  My question is, shouldn't the ThreadingMixin override this
method automatically, since if it doesn't the request socket is messed
up.

or else, have I simply configured my environment incorrectly, or am I
missing
some common knowledge piece of information regarding threads
programming?

thank you.
eric wagner.



More information about the Python-list mailing list