ANN: timeoutsocket.py --> v1.12 works with Python 2.0

David Bolen db3l at fitlinxx.com
Thu Jan 25 17:12:42 EST 2001


Rick Lee <rwklee at home.com> writes:

> I don't know how expensive threads are.  If I have to implement a
> server, it seems to me that I have to do recv() on every connection I
> accept on a separate thread.  Is that the way to go?  Is there a more
> efficient way?

Typically an application like this uses select() to wait for any of
its connections to have something to process, and then it processes
those that it needs to.  Some later Unix platforms also have a poll()
call that can be more efficient.

So you end up with a main loop (which may be a distinct thread if you
have a GUI front-end) blocking in select() except when something needs
to be done.

>                 Asyncore?  (In my application, all TCP connections are
> very long lived, there can be up to 1000 simultaneous connections.)

The asyncore module effectively packages up the loop around the select
(or poll if available), and dispatches I/O to the appropriate socket
connections as necessary.  It'll also queue up output and send it out
as it can from any of your socket writes.

It's a little different coding model when you write your top level
code using the library (since effectively you are writing small hooks
to either keep the library fed with more data to send, or to drain
data as it is received), but it can be very effective.

As to whether you can support 1000 simultaneous connections
(particularly dependent on how much data is flowing simultaneously)
will depend on a lot of things (including your OS/kernel, memory,
network stack and so on), but I do think it's technically feasible.

You may also want to look at the open/free medusa framework
(http://www.nightmare.com/medusa) which is layered on top of asyncore
to provide services such as an HTTP server.  If not useful directly, at
least it will provide some examples of coding to asyncore.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list