Skip to content

Commit

Permalink
Merge pull request #469 from gschaffner/docs/_conn
Browse files Browse the repository at this point in the history
Fix outdated documentation on _conn, on_connect, and on_disconnect
  • Loading branch information
comrumino authored Dec 18, 2021
2 parents 312644c + b0dced9 commit 983050a
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions docs/docs/services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@ If you wish to change the name of your service, or specify a list of aliases, se
The first name in this list is considered the "proper name" of the service, while the rest
are considered aliases. This distinction is meaningless to the protocol and the registry server.

Your service class may also define two special methods: ``on_connect(self)`` and
``on_disconnect(self)``. These methods are invoked, not surprisingly, when a connection
Your service class may also define two special methods: ``on_connect(self, conn)`` and
``on_disconnect(self, conn)``. These methods are invoked, not surprisingly, when a connection
has been established, and when it's been disconnected. Note that during ``on_disconnect``,
the connection is already dead, so you can no longer access any remote objects.

Other than that, your service instance has the ``_conn`` attribute, which represents the
:class:`~rpyc.core.protocol.Connection` that it serves. This attribute already
exists when ``on_connected`` is called.

.. note::
Try to avoid overriding the ``__init__`` method of the service. Place all initialization-related
code in ``on_connect``.
Expand Down Expand Up @@ -109,8 +105,11 @@ it's not the most common use case, two-sides services are quite useful. Consider
And this server::

class ServerService(rpyc.Service):
def on_connect(self, conn):
self.conn = conn

def exposed_bar(self):
return self._conn.root.foo() + "bar"
return self.conn.root.foo() + "bar"

The client can invoke ``conn.root.bar()`` on the server, which will, in turn, invoke ``foo`` back
on the client. The final result would be ``"foobar"``.
Expand Down

0 comments on commit 983050a

Please sign in to comment.