Questions about asyncore

Frank Millman frank at
Thu Jul 31 08:30:16 CEST 2008

On Jul 30, 7:50 pm, "Giampaolo Rodola'" <gne... at> wrote:
> On 30 Lug, 09:49, Frank Millman <fr... at> wrote:

Thanks again, Giampaolo, your input is really appreciated.

> I pretty much have the same overview I had before.
> As far as I can tell the only reason you want to use a thread is when
> you have to do something which requires a consistent amount of time to
> complete so that the asyncore loop gets blocked.
> The question is: is there anything like that in your code?
> If the answer is no then you don't need to use threads.
> Maybe you are just not used to the asynchronous approach where "wait
> for server to respond" or "wait for the response to be complete"
> doesn't mean that YOU have to wait.
> It means that when a specific condition occurs (e.g. some data is
> received) a certain method of the framework you're using will be
> called and then it will be up to you deciding what do to.

Maybe I am not being clear enough on my side.

I (hope I) understand the power of asnyc. I have written the server to
use async techniques, and when I get around to writing the full-blown
client, that will also use async techniques.

However, at this stage, all I want to do is write something quick and
dirty to check that the server is behaving as intended. I want to
throw some messages at it, more or less at random, and check the
responses. I found it much easier to do this with asyncore.loop
running in a separate thread.

To send a message from the main thread, I append it to a list called
self.sendData. In asyncore.dispatcher, writable() returns True if the
list contains anything, and handle_write() pops the message off the
list and sends it.

To receive messages, readable() always return True, and handle_read()
breaks up the input into individual messages and appends them to a
list called self.recvData. When the main thread is waiting for a
response, it simply loops until self.recvData contains something.

To do this asynchronously, for every test I would have to define the
detailed interaction between client and server, and write methods to
be called from within handle_read(). It could be done, but it would be
much more tedious.

Does this make sense?


More information about the Python-list mailing list