14.3. Multiprocessing Client

14.3.1. SetUp

iris.py:

from dataclasses import dataclass

@dataclass
class Iris:
    sepal_length: float
    sepal_width: float
    petal_length: float
    petal_width: float
    species: str

    def sepal_area(self):
        return self.sepal_length * self.sepal_width

    def petal_area(self):
        return self.petal_length * self.petal_width

    def total_area(self):
        return self.sepal_area() + self.petal_area()

14.3.2. Client

  • Obiekt wysyłający dane multiprocessing-client.py:

import pickle
from multiprocessing.connection import Client
from iris import Iris


flower = Iris(
    sepal_length=5.1,
    sepal_width=3.5,
    petal_length=1.4,
    petal_width=0.2,
    species='setosa'
)

payload = pickle.dumps(flower)


ADDRESS = ('localhost', 6000)
PASSWORD = b'My voice is my password, verify me.'

connection = Client(ADDRESS, authkey=PASSWORD)
connection.send(payload)
connection.send('close')
connection.close()

14.3.3. Assignments

Code 14.26. Solution
import json
from multiprocessing.connection import Client


ADDRESS = ('localhost', 6000)
PASSWORD = b'My voice is my password, verify me.'

DATA = dict(
    sepal_length=5.1,
    sepal_width=3.5,
    petal_length=1.4,
    petal_width=0.2,
    species='setosa')

data = json.dumps(DATA)

def run():
    connection = Client(ADDRESS, authkey=PASSWORD)
    connection.send(data)
    connection.send('close')
    connection.close()