Last active
July 28, 2017 17:56
-
-
Save MBoustani/e1214c0576a28842d238e17c8d4748ad to your computer and use it in GitHub Desktop.
RPC communication python calls using Zerorpc (MNIST sklearn example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from execution import Execution | |
e = Execution() | |
#digits = datasets.load_digits() | |
digits = e.execute('datasets.load_digits') | |
n_samples = len(digits.images) | |
data = digits.images.reshape((n_samples, -1)) | |
#classifier = svm.SVC(gamma=0.001) | |
classifier = e.execute('svm.SVC', args={"gamma":0.001}) | |
#classifier.fit(data[:n_samples // 2], digits.target[:n_samples // 2]) | |
classifier = e.execute('fit', args=[data[:n_samples // 2], digits.target[:n_samples // 2]], var=classifier) | |
expected = digits.target[n_samples // 2:] | |
#predicted = classifier.predict(data[n_samples // 2:]) | |
predicted = e.execute('predict', args=[data[n_samples // 2:]], var=classifier) | |
images_and_predictions = list(zip(digits.images[n_samples // 2:], predicted)) | |
for index, (image, prediction) in enumerate(images_and_predictions[:4]): | |
plt.subplot(2, 4, index + 5) | |
plt.axis('off') | |
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') | |
plt.title('Prediction: %i' % prediction) | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import zerorpc | |
client = zerorpc.Client() | |
client.connect("tcp://127.0.0.1:4242") | |
class Execution(): | |
def execute(self, function_call, args=None, var=None): | |
return pickle.loads(client.execute(function_call, pickle.dumps(args), pickle.dumps(var))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zerorpc | |
import pickle | |
#sklearn has to be imported | |
from sklearn import datasets, svm, metrics | |
def dec_func(function_name, args, var): | |
if var: | |
function_call = getattr(var, function_name) | |
return pickle.dumps(function_call(*args)) | |
else: | |
function_call = eval(function_name) | |
if args: | |
return pickle.dumps(function_call(**args)) | |
else: | |
return pickle.dumps(function_call()) | |
class Server(): | |
def execute(self, f_name, args=None, var=None): | |
args = pickle.loads(args) | |
var = pickle.loads(var) | |
return dec_func(f_name, args, var) | |
s = zerorpc.Server(Server()) | |
s.bind('tcp://127.0.0.1:4242') | |
s.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description:
I used Zerorpc as RPC communication library for client to call functions which is installed on different node (server).
If you noticed sklearn library has not been installed on client machine while is being called. The calls on client being sent to server using execution API (execution.py) and data being sent back to client using RPC too.