Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c9ce495

Browse filesBrowse files
authored
add zmq example (#422)
1 parent 835f0c5 commit c9ce495
Copy full SHA for c9ce495

File tree

Expand file treeCollapse file tree

3 files changed

+190
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+190
-0
lines changed
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This example shows how to use a zmq publisher-subscriber pattern to perform a computation in one process and visualize results in another process. First, run all cells in `multiprocessing_zmq_plot.ipynb`, and then run cells in `multiprocessing_zmq_compute.ipynb`. The raw bytes for the numpy array are sent using zmq in the compute notebook and received in the plot notebook and displayed.
2+
3+
For more information on zmq see: https://zeromq.org/languages/python/
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "ca2817a3-869c-4cc6-901b-c34509518175",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import numpy as np\n",
11+
"import zmq"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"id": "dd0b9780-b507-4ea2-af09-134abd76f45b",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"context = zmq.Context()\n",
22+
"\n",
23+
"# create publisher\n",
24+
"socket = context.socket(zmq.PUB)\n",
25+
"socket.bind(\"tcp://127.0.0.1:5555\")"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"id": "4729bfe8-3474-4bc9-a489-a57d02e5a287",
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"for i in range(2_000):\n",
36+
" # make some data, make note of the dtype\n",
37+
" data = np.random.rand(512, 512).astype(np.float32)\n",
38+
"\n",
39+
" # sent bytes over the socket\n",
40+
" socket.send(data.tobytes())"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"id": "d47dab72-1061-439f-bf6e-a88b9ee8e5aa",
47+
"metadata": {},
48+
"outputs": [],
49+
"source": []
50+
}
51+
],
52+
"metadata": {
53+
"kernelspec": {
54+
"display_name": "Python 3 (ipykernel)",
55+
"language": "python",
56+
"name": "python3"
57+
},
58+
"language_info": {
59+
"codemirror_mode": {
60+
"name": "ipython",
61+
"version": 3
62+
},
63+
"file_extension": ".py",
64+
"mimetype": "text/x-python",
65+
"name": "python",
66+
"nbconvert_exporter": "python",
67+
"pygments_lexer": "ipython3",
68+
"version": "3.11.3"
69+
}
70+
},
71+
"nbformat": 4,
72+
"nbformat_minor": 5
73+
}
+114Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "491e6050-64ae-4bfc-a480-5805cd684710",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import fastplotlib as fpl\n",
11+
"import numpy as np\n",
12+
"import zmq"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "97135f98-6810-49b6-a8de-d0e114720d6c",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"context = zmq.Context()\n",
23+
"\n",
24+
"# create subscriber\n",
25+
"sub = context.socket(zmq.SUB)\n",
26+
"sub.setsockopt(zmq.SUBSCRIBE, b\"\")\n",
27+
"\n",
28+
"# keep only the most recent message\n",
29+
"sub.setsockopt(zmq.CONFLATE, 1)\n",
30+
"\n",
31+
"# publisher address and port\n",
32+
"sub.connect(\"tcp://127.0.0.1:5555\")"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"id": "2d4420f2-364a-445a-9658-63e9ffa586c3",
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"def get_bytes():\n",
43+
" \"\"\"\n",
44+
" Gets the bytes from the publisher\n",
45+
" \"\"\"\n",
46+
" try:\n",
47+
" b = sub.recv(zmq.NOBLOCK)\n",
48+
" except zmq.Again:\n",
49+
" pass\n",
50+
" else:\n",
51+
" return b\n",
52+
" \n",
53+
" return None"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"id": "42d20d77-b884-4379-80e4-e08738506eeb",
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"plot = fpl.Plot()\n",
64+
"\n",
65+
"# initialize some data, must be of same dtype and shape as data sent by publisher\n",
66+
"data = np.random.rand(512, 512).astype(np.float32)\n",
67+
"plot.add_image(data, name=\"image\")\n",
68+
"\n",
69+
"def update_frame(p):\n",
70+
" # recieve bytes\n",
71+
" b = get_bytes()\n",
72+
" \n",
73+
" if b is not None:\n",
74+
" # numpy array from bytes, MUST specify dtype and make sure it matches what you sent\n",
75+
" a = np.frombuffer(b, dtype=np.float32).reshape(512, 512)\n",
76+
" \n",
77+
" # set graphic data\n",
78+
" p[\"image\"].data = a\n",
79+
"\n",
80+
"plot.add_animations(update_frame)\n",
81+
"plot.show()"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"id": "0f8ac188-9359-4d3c-b8f1-384be84d1585",
88+
"metadata": {},
89+
"outputs": [],
90+
"source": []
91+
}
92+
],
93+
"metadata": {
94+
"kernelspec": {
95+
"display_name": "Python 3 (ipykernel)",
96+
"language": "python",
97+
"name": "python3"
98+
},
99+
"language_info": {
100+
"codemirror_mode": {
101+
"name": "ipython",
102+
"version": 3
103+
},
104+
"file_extension": ".py",
105+
"mimetype": "text/x-python",
106+
"name": "python",
107+
"nbconvert_exporter": "python",
108+
"pygments_lexer": "ipython3",
109+
"version": "3.11.3"
110+
}
111+
},
112+
"nbformat": 4,
113+
"nbformat_minor": 5
114+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.