client derived from async_chat

Dieter Maurer dieter at handshake.de
Tue Aug 19 16:41:43 EDT 2003


Patrick Useldinger <pu> writes on Mon, 18 Aug 2003 00:20:18 +0200:
> Erik Max Francis wrote:
> 
> > I tried to run your sample but you didn't include enough material for me
> > to run it as a standalone application.  When I tried to stub out the
> > additional material you didn't include, I got connection refused errors,
> > presumably because the server wasn't running at the point your clients
> > tried to connect.
> 
> Look at the following code:
> 
> sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> #sock.setblocking(0)
> print 'connect rc=',sock.connect_ex((EBHost,EBPort))
> try:
>      sock.send(message+BLOCKEND)
>      response=sock.recv(BUFFSIZE)
> finally:
>      sock.close()
> 
> If I run it without the 'sock.setblocking(0)', it works fine. If
> uncomment that line, I receive the following error:
> 
> 
>    File "I:\My Programs\sas\sasLA0.py", line 18, in ?
>      response=sock.recv(BUFFSIZE)
> socket.error: (10035, 'The socket operation could not complete without
> blocking')

This is as it should be: "setblocking(0)" tells all socket operations
not to wait but to return immeadiately. If the operation could
not be performed, you receive this exception.

> This is exactly what seems to happen with the async_chat class, as in
> dispatcher.create_socket the same setblocking(0) is done.

Sure. "async" stands for "asynchronous".

Your program above uses a "synchronous" programming style:
you wait until the socket can perform the operation.
Once, this synchronization is done, the operation is performed
and your program continues. This style calls for "setblocking(1)".

"async_chat" on the other hand uses an "asynchronour" programming
style. Your program registers requests and waits on a main
loop (the "asyncore" main loop) when it has no more requests to do.
The "asyncore" main loop notices when one of the requests
can execute. Then, it calls back the corresponding request handler
It performs the request and may register more requests.


We have implemented clients and servers with "asyncore/async_chat".


Dieter




More information about the Python-list mailing list