[Twisted-Python] "blocking" on a response to a client

Jean-Paul Calderone exarkun at twistedmatrix.com
Tue Nov 6 06:47:59 MST 2018


On Tue, Nov 6, 2018 at 6:42 AM Chris Withers <chris at simplistix.co.uk> wrote:

> On 05/11/2018 18:32, meejah wrote:
> > Chris Withers <chris at simplistix.co.uk> writes:
> >
> >> So, separate question: I have a client that's making a connection to a
> >> websocket from a server. The client needs to send an "auth" message
> >> once the websocket is up, and the client code shouldn't progress until
> >> it receives an affirmative response. How do I do this in a
> >> non-blocking way when using twisted? (inside an inlineDeferred method
> >> in this case!)
> >
> > There's nothing really special about an @inlineCallbacks method -- it
> > just returns a Deferred so you can handle it like any other
> > Deferred-returning method.
>
> Thanks, but the special bit I was asking about is having my
> application's logical flow pause until the websocket is successfully up
> and a successful response has been received, all without blocking the
> reactor.
>
> How do I do that?
>
>
Put the rest of your code in a callback on a Deferred that fires when the
event you're interested in happens.  When using inlineCallbacks, that's
like:

    yield the_event_youre_interested_in
    # ... more code

At least, this is the extremely-localized solution to the problem.  There
are many possible ways to structure such a thing.  It's easy to describe
this one because you said you're using inlineCallbacks and didn't say
anything else about how your code is organized.

Jean-Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20181106/f6e17eee/attachment-0002.html>


More information about the Twisted-Python mailing list