Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9874b10

Browse files
author
Ryan Neufeld
committed
Merge pull request clojure-cookbook#313 from daemianmack/patch-7
Update zmq-async.asciidoc
2 parents 04786b6 + 6c5c029 commit 9874b10

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

network-io/zmq-async/zmq-async.asciidoc

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ by Kevin J. Lynagh
44

55
==== Problem
66

7-
You want to use ZeroMQ sockets in a thread safe way via core.async.
7+
You want to use ZeroMQ sockets in a thread-safe way via core.async.
88

99
==== Solution
1010

@@ -38,40 +38,41 @@ Here's a simple ping-pong between two core.async go blocks communicating via a Z
3838
(require '[com.keminglabs.zmq-async.core :refer [register-socket!]]
3939
'[clojure.core.async :refer [>! <! go chan sliding-buffer close!]])
4040
41-
(let [n 3, addr "inproc://ping-pong"
42-
[s-in s-out c-in c-out] (repeatedly 4 #(chan (sliding-buffer 64)))]
41+
(let [n 3
42+
addr "inproc://ping-pong"
43+
[server-in server-out client-in client-out] (repeatedly 4 #(chan (sliding-buffer 64)))]
4344
44-
(register-socket! {:in s-in :out s-out :socket-type :rep
45+
(register-socket! {:in server-in :out server-out :socket-type :rep
4546
:configurator (fn [socket] (.bind socket addr))})
46-
(register-socket! {:in c-in :out c-out :socket-type :req
47+
(register-socket! {:in client-in :out client-out :socket-type :req
4748
:configurator (fn [socket] (.connect socket addr))})
4849
4950
(go (dotimes [_ n]
50-
(println (String. (<! s-out)))
51-
(>! s-in "pong"))
52-
(close! s-in))
51+
(println (String. (<! server-out)))
52+
(>! server-in "pong"))
53+
(close! server-in))
5354
5455
(go (dotimes [_ n]
55-
(>! c-in "ping")
56-
(println (String. (<! c-out))))
57-
(close! c-in)))
56+
(>! client-in "ping")
57+
(println (String. (<! client-out))))
58+
(close! client-in)))
5859
----
5960

6061
==== Discussion
6162

62-
ZeroMQ is a message-oriented socket system that supports many communication styles (request/reply, , &c.) on top of many transport layers (intra-process, inter-process, inter-machine via tcp, &c.) with bindings to many languages.
63+
ZeroMQ is a message-oriented socket system that supports many communication styles (request/reply, pub-sub, etc.) on top of many transport layers (intra-process, inter-process, inter-machine via tcp, etc.) with bindings to many languages.
6364
ZeroMQ sockets are a great substrate upon which to build service-oriented architectures.
64-
ZeroMQ sockets have less overhead than HTTP and are architecturally more flexible---supporting publish/subscribe, fan-out, and other topologies in addition to request/reply.
65+
ZeroMQ sockets have less overhead than HTTP and are architecturally more flexible -- supporting publish/subscribe, fan-out, and other topologies in addition to request/reply.
6566

66-
However, ZeroMQ sockets are not thread safe---concurrent usage typically requires explicit locking or dedicated threads and queues.
67-
The zmq-async library handles all of that for you, creating ZeroMQ socket on your behalf and giving you access to them via thread safe core.async channels.
67+
However, ZeroMQ sockets are not thread-safe -- concurrent usage typically requires explicit locking or dedicated threads and queues.
68+
The zmq-async library handles all of that for you, creating ZeroMQ sockets on your behalf and giving you access to them via thread-safe core.async channels.
6869

69-
The zmq-async library provides one function, `register-socket!`, which associates a ZeroMQ socket with core.async channel(s) `in` (into which you can write strings or byte arrays) and/or `out` (whence byte arrays).
70-
Writing a Clojure collection of strings and/or byte arrays sends a multipart message; received multipart messages are put on core.async channels as a vector of byte arrays.
70+
The zmq-async library provides one function, `register-socket!`, which associates a ZeroMQ socket with core.async channel(s) `in` (into which you can write strings or byte arrays) and/or `out` (from which you can read byte arrays).
71+
Writing a Clojure collection of strings and/or byte arrays sends a multipart message. Received multipart messages are put on core.async channels as a vector of byte arrays.
7172

72-
The `register-socket!` function can be given an already created ZeroMQ socket, but typically you would have the library create a socket for you by passing the `socket-type` and a `configurator`.
73+
The `register-socket!` function can be given an already-created ZeroMQ socket, but typically you would have the library create a socket for you by passing the `socket-type` and a `configurator`.
7374
The configurator is a function that is passed the raw ZeroMQ socket object.
74-
This function is run on the socket after it is created to connect/bind addresses, set pubsub subscriptions, and otherwise configure the socket.
75+
This function is run on the socket after it is created in order to connect/bind addresses, set pub-sub subscriptions, and otherwise configure the socket.
7576

7677
WARNING: The implicit context supporting `register-socket!` can only handle one incoming/outgoing message at a time.
7778
If you need sockets to work in parallel (i.e., you don't want to miss a small control message just because you're slurping in a 10GB message on another socket), then you'll need multiple zmq-async contexts.

0 commit comments

Comments
 (0)