[go: up one dir, main page]

Add transparent P2P callbacks

Imagine the following situation:

class TestObject(LocalHERO):
    @event
    def new_data(self):
        return "foo"

# create two local objects and two remote representations of them.
alice = TestObject("alice")
bob = TestObject("bob")

remote_alice = RemoteHERO("alice")
remote_bob = RemoteHERO("bob")

# When connecting these objects with events and callbacks, we can distinguish 4 cases:

alice.new_data.connect(print)                             # case A: local object, local callback
alice.new_data.connect(remote_bob.print_from_afar)        # case B: local object, remote callback

remote_alice.new_data.connect(print)                      # case C: remote object, local callback
remote_alice.new_data.connect(remote_bob.print_from_afar) # case D: remote object, remote callback (p2p dispatch)

Even thought the four cases of communication outlined in the code sample above require very different communication pattern, the all now work with this unified interface regardless if they are remote methods or local callables. Most importantly, case D now does P2P dispatching, meaning the two remote objects directly communicate with each other in case the event is triggered rather than brokering via the instance that induced the connection.

If a callable is attached to alice, the new_data of alice (LocalHERO) gets a callback. In this case we are not interested if the callable is a local callable or something remote. (case A & case B)

Yet, if a callable func is attach to the remote representation of the event of alice remote_alice.new_data, the we decide if the callable is local or remote:

  1. func is a local callable like print. Local means local in the context of the attaching interpreter. In this case func is connected to the remote representation of new_data. Therefore the call to func is enacted by remote_alice (RemoteHERO) (case C).
  2. func is a method of a RemoteHERO like remote_bob.print_from_afar. Then, remote_bob.print_from_afar is directly attached to alice (LocalHERO) by the means of a (new) P2P connection between alice and a RemoteHERO of bob (case D).

This especially implies that these kinds of callbacks are unique. I.e. calling remote_alice.new_data.connect(remote_bob.print_from_afar) multiple times will only lead to one registered callback to bob.print_from_afar.

TODO:

  • add a docs page about the event/callback stuff
  • bump the version number
Edited by Suthep Pomjaksilp

Merge request reports

Loading