Mixing asyncore with threads

Lucio Torre lucio at movilogic.com
Mon Oct 22 18:49:45 EDT 2001


At 10:39 PM 22/10/2001 +0000, Thomas Jensen wrote:
>Hi,
>
>Is it possible/advisible to mix asyncore (or modules using it) with
>threads ?
>
>More specifically, I'd like to build a HTTP based server, which will
>serve, more or less, IO bound content most of the time (served directly
>from memory/disk/DB). For this asyncore seems ideal.
>However, in some circumstances the content will be quite CPU intensive
>(like dynamically generated PNGs from a large dataset, etc.). In this
>case a thread would be nice, to prevent blocking other trafic while
>calculating.
>The problem is, that select() will block until it sees trafic on a
>readable or writeable port (AFAIK).


select does not necesarily blocks.
the select bit goes like this: (from some of my code)

                     readable, writable, exceptional = 
select.select([conn.fd], [], [], 0.2)
                     if len(readable) > 0 :
                         tmp = readable[0].recv(1024*8)
                         # log byte count
                         conn.read_b += len(tmp)
                         conn.out_buff += tmp
                         if tmp != "":
                             conn.read_time = 
time.clock()

0.2 being the timeout for select. if you use 0, it will just never block.


>So far I've come up with the following solutions:
>(1) Wraping asyncore.poll() in a loop with a small timeout value.
>(2) Use a "dummy" port on the loopback interface for triggering
>select() once output from the thread is finished.
>
>Of the two, I like (2) best, but I keep getting the feeling that there
>must be a better way ?
>
>Perhaps I should just settle on either asyncore or threads ?


cant help you with asyncore or threads, but i know you can use select for this.

Lucio

>--
>Best Regards
>Thomas Jensen
>--
>http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list