RPC with MicroPython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
stijn
Posts: 464
Joined: Thu Apr 24, 2014 9:13 am

RPC with MicroPython

Post by stijn » Thu May 07, 2020 1:27 pm

I have 2 separate MicroPython processes communicating over sockets. A is a standard MicroPython process, B has MicroPython embedded and executes a file and then enters a loop where it waits for strings on the socket, executes them using execute_from_lexer() and sends the result back. This allows A to execute code on B i.e. 'remote procedure call'. I always did this manually by typing strings so in A I'd write

Code: Select all

a = [1, 2]
rpc.Exec('a = {}'.format(repr(a)))
print(rpc.Eval('foo(a)'))
rpc.Exec('del a[0]')
which has an effect similar of calling exec('a=[1, 2]'); eval('foo(a)'); exec('del a[0]') on B. That's a bit tedious, error prone and not super nice to use in a text editor since linting etc don't work on the strings.

So I checked if this could not be automated (like Pyro5 and similar) and was pretty happy to find out MicroPython supports enough special methods (at least on the unix port) to make a pretty nice implementation, even supporting iteration over remote objects. So now the equivalent of the code above on A is that I'm just writing actual code and it gets executed on B:

Code: Select all

rpc.a = [1, 2]
print(rpc.foo(rpc.a))
del rpc.a[0]
This might be useful for others so here's the code, including tests so it's clear what is supported: https://gist.github.com/stinos/b2ce4093 ... 49cc5059c9 (doesn't include the actual socket code since that's written in C++)

Post Reply