0

I seem to misinterpret the rules of dataReceived and inlineCallbacks.

More specifically, when I do this:

def dataReceived(self, data):
    self.transport.write('ok')

my Protocol really sends the data, but when I do this:

@defer.inlineCallbacks
def dataReceived(self, data):
    a = yield True # this stands for some adbapi call actually
    self.transport.write('ok')

I receive nothing, then connection just closes. Nor the defer.returnValue helps here, behaviour is just similar.

Kindly explain.

Eugene
  • 656
  • 8
  • 20
  • I just saw your question on the IRC. As stated there, can you provide a bit more insight into your code? Maybe a simple application that shares resemblance to your actual code. We can give you better help that way. – notorious.no May 27 '16 at 14:09

1 Answers1

1

This is just a hunch, but I don't think the dataReceived() function is expected to return anything, so an inlineCallbacks or even returning/yielding a regular Deferred won't really "do anything". Sorry for the ambiguity. What you probably should do is create a Deferred and start a callback chain in your dataReceived() function. For example:

def dataReceived(self, data):
    # ...
    deferredObj = adbapi.runQuery('SELECT * FROM ...')    # this returns a Deferred
    deferredObj.addCallback(self.someCallback)            # exec self.someCallback() after query returns
    deferredObj.addErrback(self.anotherCallback, *args, **kwargs)

def someCallback(self, result):
    """
    When the db query returns, do something useful here
    """
    print(result)

Links

notorious.no
  • 4,919
  • 3
  • 20
  • 34
  • That's what I came up with in the end of that discussion : unpack messages to queue, process the queue with a single deferred one message per time, drop the queue and connection altogether if I find the current message unwanted. – Eugene May 28 '16 at 08:28