On Fri, Jan 10, 2025 at 10:39 AM Kirill Miazine <k...@krot.org> wrote:

> I'm playing with PB, and it's booth cool and somewhat confusing. So I
> have some remote_* methods, e.g.
>
> def remote_ping(self):
>     return 'pong'
>
> def remote_run(self):
>     d = Deferred()
>     d.AddCallback(self.got_result)
>     # return d # <- magic here when deferred is returned?
>
> def got_result(self, res)
>     return res
>
> When I do callRemote('ping') and execute callback, I am getting 'pong'
> as a result. However, when I do callRemote('run'), it returns a
> deferred which returns None, which is expected, as remote_run()
> doesn't have any result yet.
>
> However, if I return deferred ("d") from remote_run, then
> callRemote('run') gives me
> the result. Is it some expected magic going on when remote_ method returns
> a deferred?
>

First, while PB probably is objectively pretty cool, you almost certainly
shouldn't use it.  The complexity isn't warranted except perhaps for a very
few specific kinds of applications.  Even for those applications, the level
of development on PB means that many features you will likely want aren't
available and even those features which are available have implementation
shortcomings that aren't likely to be addressed.  *IMO* the entire package
should have been split out of Twisted long ago.

As implied by the above, the right choice of protocol for your application
does depend on your application's specific requirements.  That said, HTTP
is a pretty good fit for a wide range of common application types.  Another
protocol Twisted supports, AMP, covers some more ground - though you might
find wider support for similar protocols such as those based on ProtoBufs
or msgpack (AMP is a Twisted invention and though it has a number of very
nice properties, you won't find many people using it).

To answer the question you asked, there is a limited amount of magic going
on that allows you to return a Deferred and have the method behave as
though it returned the result that Deferred eventually fires with,
instead.  This is a common pattern when developing with Twisted as it makes
dealing with asynchronous implementations more convenient.  In fact, it's
essentially the reason Deferred exists at all.

You'll find this feature in Twisted's implementation of AMP and HTTP and
many other protocols as well.

Jean-Paul
_______________________________________________
Twisted mailing list -- twisted@python.org
To unsubscribe send an email to twisted-le...@python.org
https://mail.python.org/mailman3/lists/twisted.python.org/
Message archived at 
https://mail.python.org/archives/list/twisted@python.org/message/OCSYNCH5AXRT5J43QD3QO3RDJX43YNDZ/
Code of Conduct: https://twisted.org/conduct

Reply via email to