-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from radical-cybertools/feature/zmq_examples
add complete ru zmq examples
- Loading branch information
Showing
2 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import time | ||
|
||
import threading as mt | ||
|
||
import radical.utils as ru | ||
|
||
CHANNEL = 'test' | ||
TOPIC = 'test' | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
def put(channel, uid, url, n): | ||
|
||
put = ru.zmq.Putter(channel, url) | ||
for i in range(n): | ||
put.put(ru.as_bytes('%s: message %d' % (uid, i))) | ||
print('%s: message %d' % (uid, i)) | ||
time.sleep(0.1) | ||
|
||
put.put(ru.as_bytes('%s: STOP' % uid)) | ||
|
||
print('%s: done' % uid) | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
def get(channel, uid, url): | ||
|
||
cont = True | ||
get = ru.zmq.Getter(channel, url) | ||
|
||
while cont: | ||
|
||
msgs = get.get() | ||
if not msgs: | ||
continue | ||
|
||
print('%s: %s' % (uid, ru.as_string(msgs))) | ||
|
||
for msg in msgs: | ||
if 'STOP' in ru.as_string(msg): | ||
cont = False | ||
|
||
print('%s: done' % uid) | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
def main(): | ||
|
||
bridge = ru.zmq.Queue(CHANNEL) | ||
bridge.start() | ||
|
||
threads = list() | ||
|
||
# start some putters and getters | ||
# NOTE: start more putters than getters for clean termination | ||
for i in range(4): | ||
t = mt.Thread(target=put, args=(CHANNEL, 'put.%d' % i, bridge.addr_put, 5)) | ||
t.daemon = True | ||
t.start() | ||
threads.append(t) | ||
|
||
for i in range(3): | ||
t = mt.Thread(target=get, args=(CHANNEL, 'get.%d' % i, bridge.addr_get)) | ||
t.daemon = True | ||
t.start() | ||
threads.append(t) | ||
|
||
# wait for completion | ||
for t in threads: | ||
t.join() | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
if __name__ == '__main__': | ||
|
||
main() | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import time | ||
|
||
import threading as mt | ||
|
||
import radical.utils as ru | ||
|
||
CHANNEL = 'channel' | ||
TOPIC = 'test' | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
def pub(uid, url, topic, n): | ||
|
||
pub = ru.zmq.Publisher(topic, url) | ||
for i in range(n): | ||
pub.put(topic, ru.as_bytes('%s: message %d' % (uid, i))) | ||
print('%s: message %d' % (uid, i)) | ||
time.sleep(0.1) | ||
|
||
pub.put(topic, ru.as_bytes('%s: STOP' % uid)) | ||
|
||
print('%s: done' % uid) | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
def sub(uid, url, topic): | ||
|
||
cont = True | ||
sub = ru.zmq.Subscriber(topic, url) | ||
sub.subscribe(topic) | ||
|
||
while cont: | ||
topic, msg = sub.get() | ||
print('%s: %s' % (uid, ru.as_string(msg))) | ||
|
||
if 'STOP' in ru.as_string(msg): | ||
cont = False | ||
|
||
print('%s: done' % uid) | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
def main(): | ||
|
||
bridge = ru.zmq.PubSub(CHANNEL) | ||
bridge.start() | ||
|
||
threads = list() | ||
|
||
|
||
# start some subscribers and publishers | ||
for i in range(3): | ||
t = mt.Thread(target=sub, args=('sub.%d' % i, bridge.addr_sub, TOPIC)) | ||
t.daemon = True | ||
t.start() | ||
threads.append(t) | ||
|
||
for i in range(4): | ||
t = mt.Thread(target=pub, args=('pub.%d' % i, bridge.addr_pub, TOPIC, 5)) | ||
t.daemon = True | ||
t.start() | ||
threads.append(t) | ||
|
||
|
||
# wait for completion | ||
for t in threads: | ||
t.join() | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# | ||
if __name__ == '__main__': | ||
|
||
main() | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
|