Usage¶
Core Concepts¶
app-net uses peer-to-peer socket servers to execute code both locally and remotely. The first thing to understand is the difference between local and remote execution of a function.
Local¶
When you launch python and you execute a function, it will execute inside that instance, obviously. app-net requires you the developer to define the peer id to execute the function on. If you don’t tell the function where to execute the code, it will default to a normal pass-through. This makes development and testing easier. The response locally is expected to match a remote peers response.
Remote¶
When you execute a function, you can tell it to connect to a different instance of python, execute the code, and return the result through the socket response. The thing to understand is that a remote instance doesn’t need to be on another host. Meaning, if you have 2 instances of python running app-net on the same host, they can communicate the same way they would if they were on a different host.
A Basic Example¶
This is a very simple example of an application running on 2 different peers and communicating through a shared coding contract, the application itself.
app.py¶
# imports
import net
@net.connect()
def connected_function(message):
"""
This will simply print the message passed on the local peer.
"""
print(message)
peer1.py¶
# Importing the application code will automatically launch the peer and begin
# listening for connection requests as well as set up the connection registry.
import app
if __name__ == '__main__':
# enter an endless loop so the peer will listen.
while 1:
pass
peer2.py¶
# net imports
import net
# Importing the application code will automatically launch the peer and begin
# listening for connection requests as well as set up the connection registry.
import app
if __name__ == '__main__':
all_peers = net.peers(on_host=True)
# Gets all the peers on the local network. This will return a dictionary
# where the key is the Peer.id of the peer and value is a dictionary of
# information about that peer. In this case, we know that both peers are
# running on the same host so we want to pass in the no_host=True. The
# result will be the same, but this will be significantly faster since it is
# not going to be searching the whole network.
#
# Example response:
# {
# 'peers': {
# b'MTkyLjE2OC4yLjI0OjMwMTAgLT4gTm9uZQ==': {
# 'group': 'None',
# 'host': '192.168.2.24',
# 'port': 3010,
# 'hub': False
# },
# },
# 'None': [
# b'MTkyLjE2OC4yLjI0OjMwMTAgLT4gTm9uZQ=='
# ]
# }
target_peer = all_peers['None'][0]
# since we know there is only one other peer running our application in the
# "None" group, we can assume it is safe to grab the first key. This will be
# how net knows where to execute our application. Just a note, by default, a
# peer that was initialized without a group set in its environment is set to
# the "None" group.
app.connected_function("My Message!", peer=target_peer)
# now we can call our applications function. Since this function is
# connected through net, we can pass in the keyword argument 'peer' to
# specify where to execute the function. If you do not specify the peer, it
# will simply execute the code locally as though it was not connected.
# Basically just a normal function.
# Running this will now print "My Message!" on peer1.