How to handle connection refused exception?

11 views
Skip to first unread message

Gustaw

unread,
Jun 18, 2010, 6:10:50 PM6/18/10
to diesel users
How to handle connection problems in Client?
For example if server is not started or I have some network problems.

Example:

from diesel import Application, Loop, log, Client
from diesel.core import ClientConnectionError

def req_loop():
client = Client()
try:
yield client.connect('127.0.0.1', 2775)
except ClientConnectionError:
log.info( "error1" )
a.halt()

a = Application()
log = log.sublog('client', log.info)
a.add_loop(Loop(req_loop))
a.run()

does not work and instead of "error1" I see something like this:

=== DIESEL ERROR ===

Generator stack at time of error:

<code object req_loop at 0xb7e5a848, file "http_client.py", line 4>
<code object _real_connect at 0xb7c4eba8, file "build/bdist.linux-i686/
egg/diesel/client.py", line 84>


Standard Traceback:

Traceback (most recent call last):
File "build/bdist.linux-i686/egg/diesel/core.py", line 244, in
cycle_all
item = self.current.throw(*error)
File "build/bdist.linux-i686/egg/diesel/client.py", line 104, in
_real_connect
yield cobj
ClientConnectionError: [Errno 111] Connection refused

Regards,
Gustaw

Eric Larson

unread,
Jun 18, 2010, 7:23:19 PM6/18/10
to diesel...@googlegroups.com
At Fri, 18 Jun 2010 15:10:50 -0700 (PDT),

Gustaw wrote:
>
> How to handle connection problems in Client?
> For example if server is not started or I have some network problems.
>
> Example:
>
> from diesel import Application, Loop, log, Client
> from diesel.core import ClientConnectionError
>
> def req_loop():
> client = Client()
> try:
> yield client.connect('127.0.0.1', 2775)
> except ClientConnectionError:
> log.info( "error1" )
> a.halt()
>
> a = Application()
> log = log.sublog('client', log.info)
> a.add_loop(Loop(req_loop))
> a.run()
>

Have you tried wrapping the a.halt() in a try/except as well? I've
found when doing things like while True sometimes having the try/catch
in the loop doesn't work, but wrapping the entire loop does.

Eric

Gustaw

unread,
Jun 19, 2010, 4:44:21 AM6/19/10
to diesel users
This doesn't work without a.halt() in my example or with a.halt() in
"except" block and I'm not using "while True:"

Gustaw

Gustaw

unread,
Jun 21, 2010, 5:49:02 PM6/21/10
to diesel users
answer - use "catch":

yield catch(client.connect('127.0.0.1', 2775), ClientConnectionError)

Gustaw

Eric Larson

unread,
Jun 21, 2010, 6:01:59 PM6/21/10
to diesel...@googlegroups.com
At Mon, 21 Jun 2010 14:49:02 -0700 (PDT),

Gustaw wrote:
>
> answer - use "catch":
>
> yield catch(client.connect('127.0.0.1', 2775), ClientConnectionError)
>
> Gustaw

Great! Thanks for finding that.

Eric

Reply all
Reply to author
Forward
0 new messages