9.24. Multiprocessing Client¶
9.24.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()
9.24.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()
9.24.3. Assignments¶
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()