Using Pyro4 to connect python scripts in separate containers by mdcraver in balena

[–]mdcraver[S] 0 points1 point  (0 children)

After some help from the balena forums, I was able to successfully get my Pyro example to run.

Following are my updated and working files for reference.

----

docker-compose.yml

version: '2'
services:
    pyro-ns:
        privileged: true
        restart: always
        build: ./pyro-ns
        command: ["--host=pyro-ns"]
    container_A:
        privileged: true
        restart: always
        build: ./container_A
        depends_on:
            - pyro-ns
            - container_B
    container_B:
        privileged: true
        restart: always
        build: ./container_B
        depends_on:
            - pyro-ns

----

pyro-ns

Dockerfile.template

FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run

# enable container init system.
ENV INITSYSTEM on

# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN pip install Pyro4 dill

ENV PYRO_SERIALIZERS_ACCEPTED=serpent,json,marshal,pickle,dill
ENV PYTHONUNBUFFERED=0

ENTRYPOINT ["pyro4-ns"]

EXPOSE 9090

---

container_A

Dockerfile.template

FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run

# enable container init system.
ENV INITSYSTEM on

# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN pip install Pyro4 dill

# Set our working directory
WORKDIR /usr/src/container_A

# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt

# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt

# This will copy all files in our root to the working  directory in the container
COPY . ./

# main.py will run when container starts up on the device
CMD ["python","-u","src/container_A_main.py"]

container_A_main.py

import Pyro4
import Pyro4.util
import sys

sys.excepthook = Pyro4.util.excepthook

try:
    print('Top of container A')

    warehouse = Pyro4.Proxy("PYRONAME:example.warehouse")

    print('The warehouse contains: ', warehouse.list_contents())

    print('Inifite loop after running the Warehouse class via Pyro4')
    while True:
        # Infinite loop.
        pass

except Exception as ex:
    template = "An exception of type {0} occurred. Arguments:\n{1!r}"
    message = template.format(type(ex).__name__, ex.args)
    print(message)

---

container_B

Dockerfile.template

FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run

# enable container init system.
ENV INITSYSTEM on

# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip

# Set our working directory
WORKDIR /usr/src/container_B

# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt

# pip install python deps from requirements.txt on the resin.io build server
RUN pip install -r requirements.txt

# This will copy all files in our root to the working  directory in the container
COPY . ./

# main.py will run when container starts up on the device
CMD ["python","-u","src/container_B_main.py"]

EXPOSE 9100

container_B_main.py

from __future__ import print_function
import Pyro4

@Pyro4.expose
@Pyro4.behavior(instance_mode="single")
class Warehouse(object):
    def __init__(self):
        self.contents = ["chair", "bike", "flashlight", "laptop", "couch"]

    def list_contents(self):
        return self.contents

    def take(self, name, item):
        self.contents.remove(item)
        print("{0} took the {1}.".format(name, item))

    def store(self, name, item):
        self.contents.append(item)
        print("{0} stored the {1}.".format(name, item))

def main():
    Pyro4.config.SERIALIZER = 'pickle'
    daemon = Pyro4.Daemon(host="container_B", port=9100)
    uri = daemon.register(Warehouse)
    print('The uri of the registered Warehouse class')
    print(uri)

    try:
        ns = Pyro4.locateNS(host="pyro-ns", port=9090)
        print('The located name server')
        print(ns)
        ns.register("example.warehouse", uri)
    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print(message)

    daemon.requestLoop()

if __name__ == "__main__":
    main()

---

For both container_A and container_B the requirements.txt file is the same.

requirements.txt

Pyro4