What is wrong with this code?

296 views Asked by At

I'm using the code below to slug the link to a couchDb instance, so I can test what happens to the GUI if the database is slow.

# coding=utf8   slowCoach.py - a server that delays every message 
#      thus making the database crawl. 
import eventlet
from time import sleep
def closed_callback():
    print "called back"
def forward(source, dest, cb = lambda: None):
    """Forwards bytes unidirectionally from source to dest"""
    while True:
        d = source.recv(32384)
        if d == '':
            cb()
            break
        sleep(0.3)    
        dest.sendall(d)
listener = eventlet.listen(('localhost', 5981 ))
while True:
    client, addr = listener.accept()
    server = eventlet.connect(('192.168.0.1',5984))
    # two unidirectional forwarders make a bidirectional one
    eventlet.spawn_n(forward, client, server, closed_callback)
    eventlet.spawn_n(forward, server, client)

The code is based upon the port forwarder example.

It works as expected when I read, delete, and update a document, but when I insert the GUI hangs. When I link direct, missing out the code above, inserts work just fine!

The Python code that is talking to the database is couchDB-python (http://code.google.com/p/couchdb-python/) and BOTH save and insert execute the following statement

    try: 
        self.db.save(dic)
    except couchdb.ResourceConflict: # wrong _rev - updated by someone else
        return False
    for fld in ['_id','_rev']:  # set id and rev to current values 
        value = dic[fld]
        setattr(self, fld, value)
    self._status = CouchObject.CLEAN
    return self

Yet one works and the other doesn't. Does anyone have any hint on how to debug this, or what might be going wrong?

1

There are 1 answers

0
JasonSmith On BEST ANSWER

Ian says:

To answer my own question - I should have imported sleep from eventlet. Without that the green threads lock, and stuff is lost. I also had similar problem in caller, which sent next message to couch before last recieved, and who knows who got what reply!