Connection on Python driver

193 views
Skip to first unread message

Kless

unread,
May 9, 2009, 12:37:26 PM5/9/09
to Redis DB
There is a little issue in the Python driver.

It's called 'self.connect()' from several functions to connect to the
server (before of run the command). But it's giving failures because
it doesn't wait until that the conection is ready.

A possible solution would be wait until that the server send you an
answer.

Kless

unread,
May 9, 2009, 12:50:55 PM5/9/09
to Redis DB
The problem is on the 'connect' function that doesn't wait until that
there would be a connection. I'm supposed that there is not a way to
know it. This could be fixed if the server send an answer with a code
that indicates it.

Kless

unread,
May 10, 2009, 4:49:42 AM5/10/09
to Redis DB
This is the error showed when the client is disconnected:

...........................................
>>> r.keys('*')

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (446, 0))

---------------------------------------------------------------------------
IndexError Traceback (most recent call
last)

/home/neo/<ipython console> in <module>()

/opt/Development/django-env/lib/python2.6/site-packages/redis.pyc in
keys(self,
pattern)
268 self.connect
()
269 self._write('KEYS %s\r\n' %
pattern)
--> 270 return self.get_response().split
()

271
272 def randomkey
(self):

/opt/Development/django-env/lib/python2.6/site-packages/redis.pyc in
get_response
(self)
881 def get_response
(self):
882 data = self._read().strip
()
--> 883 c = data
[0]
884 if c ==
'-':
885 raise ResponseError(data[5:] if data[:5] == '-ERR
' else data
[1:])

IndexError: string index out of range
...........................................

If I try again, it shows this now:
...........................................
>>> r.keys('*')

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (447, 0))

---------------------------------------------------------------------------
ConnectionError Traceback (most recent call
last)

/home/neo/<ipython console> in <module>()

/opt/Development/django-env/lib/python2.6/site-packages/redis.pyc in
keys(self,pattern)
267 """
268 self.connect()
--> 269 self._write('KEYS %s\r\n' % pattern)
270 return self.get_response().split()
271

/opt/Development/django-env/lib/python2.6/site-packages/redis.pyc in
_write(self, s)
61 # broken pipe

62 self.disconnect()
---> 63 raise ConnectionError("Error %s while writing to
socket. %s." % tuple(e.args))
64
65 def _read(self):

ConnectionError: Error 32 while writing to socket. Broken pipe.
...........................................

And it connects after of three times.

ludo

unread,
May 11, 2009, 5:11:03 AM5/11/09
to Redis DB
On May 9, 6:50 pm, Kless <jonas....@googlemail.com> wrote:
> The problem is on the 'connect' function that doesn't wait until that
> there would be a connection. I'm supposed that there is not a way to
> know it. This could be fixed if the server send an answer with a code
> that indicates it.

Strange, as it's a synchronous call, same as all the others made on
the socket.

What you should do is trap the ConnectionError exception, then do what
your app needs done (log a message, restart the connection, exit,
whatever).

I might be mistaken, but I think the problem lies in your setup.

L.

Kless

unread,
May 11, 2009, 6:39:27 AM5/11/09
to Redis DB


On 11 mayo, 09:11, ludo <l...@qix.it> wrote:
> Strange, as it's a synchronous call, same as all the others made on
> the socket.
And the most strange is that it happens only when the client is
disconnected automatically (after of X time without access to the
server). If I disconnect it, and call a coomand then there is not
problem.

Could test it waiting until that your client been disconnected?

---------------------------
In [1]: import redis

In [2]: r = redis.Redis()

In [3]: r.keys('*')
Out[3]: []

In [4]: r.disconnect()

In [5]: r.keys('*')
Out[5]: []
---------------------------
Wait until that it been disconnected and try:

>>> r.keys('*')


> I might be mistaken, but I think the problem lies in your setup.
I think that no since that I use the configuration by default:

http://github.com/antirez/redis/blob/ecfaf6da92d8d482460d9491a69d80f91b2af7c0/redis.conf

Kless

unread,
May 17, 2009, 6:15:33 AM5/17/09
to Redis DB

On 11 mayo, 09:11, ludo <l...@qix.it> wrote:
> What you should do is trap the ConnectionError exception, then do what
> your app needs done (log a message, restart the connection, exit,
> whatever).
I think that it's the python driver which must manage that issue. It
should be to disconnect and then to try again.

I cann't help much since that I'm not an expert on socket programming
but this code would help for that the disconnection been managed from
the '_read' function:

-------------------
def get_response(self):
while 1:
data = self._read().strip()
if data:
break
-------------------

http://github.com/antirez/redis/commit/b062edf392a70116031881f9af7a082d042a2dbd#diff-0
Reply all
Reply to author
Forward
0 new messages