example: hello world - modrpc/info GitHub Wiki

We already tried a simple HelloWorld example to test the ModPy installation. In this example, we will try a bit more complicated example with some twist. Rather than using the ModPy shell, we will create two ModPy nodes.

First Node

   # hello.py
   import modpy

   # ModPy initial functions are executed only once when the node starts
   @modpy.initial
   def onboard():
      # use of "*" in URL means that this call will be broadcasted to all
      # nodes in the local network
      await modpy.call("*/sayhello", modpy.self_nodename())

   @modpy.func
   def sayhello(nodename):
     return "Hello from " + nodename

   node = modpy.init_node().start()
  • Now let's try launching two nodes with different names. Each command should be run in different terminals. Be sure to use different RPC port.
   # run on terminal #1
   $ python3 hello.py --name polya --rpcport 12345

   # run on terminal #2
   $ python3 hello.py --name gauss --rpcport 12346

Client Node

  • Client program
   # client.py
   import modpy

   node = modpy.init_node()
   node.thr_start()
   result = modpy.thr_call(node, "polya/hello", "world")
   print(result)
  • Be sure to use a different RPC port to avoid conflict.
   $ python3 client.py -name erdos -rpcport 12346
⚠️ **GitHub.com Fallback** ⚠️