Debugging code using the cassandra driver in Pycharm

401 views
Skip to first unread message

Kostas Chalikias

unread,
Apr 15, 2015, 8:47:05 AM4/15/15
to python-dr...@lists.datastax.com
I am using PyCharm 4.0.5 with python 2.7.6 and the cassandra-driver library 2.5.0 and I am having trouble running code that calls Cluster.connect() from within the debugger *only* ie the same code works when using Run -> Run instead of Run -> Debug. The exception thrown is 

NoHostAvailable: ('Unable to connect to any servers', {'127.0.0.1': OperationTimedOut('errors=None, last_host=None',)})

At first I thought maybe the control_connection_time (defaulted to 2) was being hit because code in the debugger runs slower but increasing only causes the process to wait for longer before timing out with the same message. 

Looking around the driver code, I have been able to reproduce the problem outside it using the following example which seems equivalent to how the driver waits for a response when calling connection.register_watchers in cluster.py:1922

from threading import Thread, Event


event = Event()


class TestFoo(object):


   
def test_foo(self):
       
def runnable():
           
print('other thread starting')
           
event.set()
           
print('other thread called set')


        t
= Thread(target=runnable)
        t
.start()


       
print('main thread will wait')
       
event.wait(1000)
       
print('main thread woken up')




If I step through this in PyCharm in the debugger it will end up waiting for 1000 seconds but when I just run it it runs through immediately. I find it hard to believe the driver simply doesn't work in the debugger so any ideas are welcome! Thanks

Adam Holmberg

unread,
Apr 15, 2015, 12:10:19 PM4/15/15
to python-dr...@lists.datastax.com
Thanks for trying to isolate your issue. That's helpful in troubleshooting.

I didn't observe the same problem running your posted script, but I did see the issue trying connect the driver. 

This points to an issue with the way in which the driver chooses its default connection class. Apparently PyCharm is loading gevent when debugging, causing the default connection class to be GeventConnection (even though you have presumably not monkey patched). I've opened an issue to make this logic more sophisticated.

In the mean time, you can work around this by passing an explicit connection_class to Cluster:

from cassandra.io.libevreactor import LibEvConnection
from cassandra.io.libevreactor import LibevConnection
session = Cluster(connection_class=LibevConnection).connect()
Alternatively, if you run in a virtualenv without gevent installed, this also should not happen.

Regards,
Adam Holmberg

To unsubscribe from this group and stop receiving emails from it, send an email to python-driver-u...@lists.datastax.com.

Kostas Chalikias

unread,
Apr 15, 2015, 12:36:18 PM4/15/15
to python-dr...@lists.datastax.com
Thanks, that does the trick! The independent repro was in fact a red herring in that if you step through every line in the main thread AND add a breakpoint to the second thread (which is hit when it starts), you will hit the sleep and the other thread will remain suspended, pycharm doesn't let you mess with it in the meanwhile. So you have to wait the full sleep time to let either of the two go.
To unsubscribe from this group and stop receiving emails from it, send an email to python-driver-user+unsub...@lists.datastax.com.

Reply all
Reply to author
Forward
0 new messages