SocketServer - multithreading

Oleg Broytmann phd at phd.pp.ru
Thu Oct 11 11:38:04 EDT 2001


Hi!

On Thu, Oct 11, 2001 at 11:22:28AM -0400, Jeremy Jones wrote:
> I am trying to write a simple daemon that listens on a port and writes the
> request that it receives to a file.  There will likely be multiple requests
> coming in at the same time, so it needs to block the file writing so that
> only one request writes to a file at once.  I "borrowed" a code example from
> the web and hacked it a bit to try to do what I need  (and it almost works).
> (If anyone sees that I am going about this in the wrong manner, please say
> so.  I have had no experience with threading and almost no experience with
> sockets.  Pointing me in another direction would be most welcome.)  Assuming
> that SocketServer is a good way to handle this, here is what I have so far
> (BTW - this is Python 1.5.2 on Linux):

   Wow! The code looks VERY familiar to me! :)))

> """
> #! /usr/bin/python -O
> 
> 
> import sys, SocketServer
> import string
> 
> PORT = 2100
> 
> 
> class RequestServer(SocketServer.ThreadingTCPServer):
>    allow_reuse_address = 1
> 
> 
> class RequestHandler(SocketServer.StreamRequestHandler):
>    def handle(self):
>       host, port = self.client_address
>       #print "Connected by", host, port
> 
>       # get data
>       request = ''
> 
>       while 1:
>          line = self.rfile.readline()
>          if line in (None, "\n", "\r\n"):
>             break
>          request = request + line
> 
>       self.server.request = request
>       request = string.rstrip(request)
>       print request
> 
>       #outfile = open("server.csv", 'w')
>       #outfile.write(request)
>       #outfile.close()
>  
> 
> def main():
>    # get port
>    if len(sys.argv) > 1:
>       port = int(eval(sys.argv[1]))
>    else:
>       port = PORT
> 
>    print "Waiting..."
>    server = RequestServer(('', port), RequestHandler)
>    #server.handle_request() # do not server forever - serve just 1 request
>    server.serve_forever() # serve forever
> 
> main()
> """
> 
> The thing that isn't working is the file writing (thus it's commented out
> right now).  With the 3 "outfile" lines commented out, this works fine.  But
> when I uncomment them, the first request prints the request to STDOUT and to
> the file, but subsequent requests only print to STDOUT.  The first and
> subsequent requests apparently sit waiting on the socket:
> 
> [jjones at mahler socket]$ netstat -a | grep 2100
> tcp        0      0 mahler.atlqa:2100       mahler.atlqa:3167
> TIME_WAIT   
> tcp        0      0 mahler.atlqa:2100       mahler.atlqa:3166
> TIME_WAIT   
> tcp        0      0 mahler.atlqa:2100       mahler.atlqa:3165
> TIME_WAIT   
> tcp        0      0 *:2100                  *:*                     LISTEN
> 
> If anyone has any ideas how I can get this working (or find another
> solution), I would be most grateful.

   Do you run the program in the background? It is possible that the
program stuck upon printing the request, because background programs
usually disallowed to write to console. Upon printing the program receives
SIGTSTOP signal and stops.
   Do not write to stdout from a background process.

Oleg.
---- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.




More information about the Python-list mailing list