Skip to content

Instantly share code, notes, and snippets.

@h2suzuki
Last active February 19, 2017 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h2suzuki/f9ccbeb75877cd5381820f37f2164617 to your computer and use it in GitHub Desktop.
Save h2suzuki/f9ccbeb75877cd5381820f37f2164617 to your computer and use it in GitHub Desktop.
import zmq
import numpy as np
import json
import time
# ZeroMQ のバックグラウンド・スレッドのコンテキスト
context = zmq.Context()
# このクライアントは、ポート5556に接続します(バックグラウンドにて)
socket = context.socket(zmq.REP)
socket.connect("tcp://localhost:5556")
# シリアライズされた Numpy Array を受信
[dtype, shape, data] = socket.recv_multipart()
# Numpy Array にデシリアライズ
M = np.frombuffer(data, dtype=dtype.decode('utf-8'))
M.shape = json.loads(shape.decode('utf-8'))
print("Request:\n{} received".format(M))
# なにか計算してみる; 各要素に+1
M = M + 1
# ちょっと待ってみる
time.sleep(1)
# 計算結果を返信
socket.send_multipart([dtype, shape, M.tostring('C')])
print("Reply:\n{} sent".format(M))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment