Problem in multithreading Socketserver explained (should appl y to *nix, too)

Michael Abbott Michael at RCP.co.uk
Thu Aug 30 03:02:23 EDT 2001


(I'm not on python-list; evidently need to change this.  In fact, where is
this list?  It doesn't appear on http://www.python.org/psa/MailingLists.html
)

This is the source in my version of Python: the interpreter comes up
reporting 

Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32

I don't understand the solution you're describing, unless process_request is
now guaranteed to call close_request.  My solution was to split the handling
into two parts:

	1.	Get the incoming request; this is synchronous
	2.	Process the received request; it should be possible to move
*all* this processing into a separate thread.

The 2.1.1 solution you quote doesn't look right to me, in particular cleanup
and error handling in the presence of a separate thread looks doubtful.

-----Original Message-----
From: brueckd at tbye.com [mailto:brueckd at tbye.com]
Sent: 29 August 2001 17:20
To: Michael Abbott
Cc: python-list at python.org
Subject: Re: Problem in multithreading Socketserver explained (should
apply to *nix, too)


On Wed, 29 Aug 2001, Michael Abbott wrote:

> > def handle_request(self):
> >     try:
> >         request, client_address = self.get_request()
> >     except socket.error:
> >         return
> >     if self.verify_request(request, client_address):
> >         try:
> >             # this will start a new thread
> >             self.process_request(request, client_address)
> >         except:
> >             self.handle_error(request, client_address)
> >     # this will close the socket still in use by the new thread
> >     self.close_request(request)

What version of Python is this? 1.5.2 and 2.0 don't have the close_request
call at all. Python 2.1.1 has:

  ...
  try:
    self.process_request(request, client_address)
  except:
    self.handle_error(request, client_address)
    self.close_request(request)

Note the indentation - the close_request call happens only after an
exception gets raised, and so the threading mix-in works fine. Has yours
been modified or do I just have a different version than you?

> > Solution:

Instead of that long solution why don't you just change the indentation of
close_request? Or, subclass TCPServer and implement your own
handle_request that doesn't call close_request at the end. Then you can
use the threading mixin. Either way, it's a lot less work.

-Dave




More information about the Python-list mailing list