Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add working pyzmq socket support #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv
__pycache__
58 changes: 58 additions & 0 deletions examples/closing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import trio
import trzmq
import zmq

async def req(s, lock, n):
i = 1
while True:
topic = b"ZMQ-Test"
message = f"Hello {i}".encode()
async with lock:
await s.send(b"%b %b" % (topic, message))
print(f"{n} Sent: {topic} {message}")
reply = await s.recv()
print(f"{n} Got response: {reply}")
i += 1
await trio.sleep(1)
if n == 1 and i > 30:
s.close()
break

async def rep():
await trio.sleep(10)

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.setsockopt(zmq.SNDHWM, 2)
socket.bind("tcp://0.0.0.0:5556")

s = trzmq.Socket(socket)
i = 1
while True:
msg = await s.recv()
await s.send(b'Reply to: ' + msg)
if i > 30:
s.close()
break

async def printer():
while True:
print("Still alive")
await trio.sleep(1)

async def run():
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.SNDHWM, 5)
socket.connect("tcp://0.0.0.0:5556")

s = trzmq.Socket(socket)
lock = trio.Lock()
async with trio.open_nursery() as nursery:
nursery.start_soon(req, s, lock, 1)
nursery.start_soon(req, s, lock, 2)
nursery.start_soon(rep)
nursery.start_soon(printer)


trio.run(run)
22 changes: 22 additions & 0 deletions examples/pub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# publisher

import trzmq
import trio
import zmq

async def run():
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.connect("tcp://0.0.0.0:5556")

s = trzmq.Socket(socket)
i = 1
while True:
topic = b"ZMQ-Test"
message = "Hello, NORM " + str(i) + "..."
await s.send(b"%b %b" % (topic, message.encode()))
print("%s %s" % (topic, message))
i += 1
await trio.sleep(1)

trio.run(run)
41 changes: 41 additions & 0 deletions examples/pubsub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import trzmq
import trio
import zmq


async def pub(n):
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.connect("tcp://0.0.0.0:5556")

s = trzmq.Socket(socket)
i = 1
while True:
topic = b"ZMQ-Test"
message = f"Hello, from {n}: {i}..."
await s.send(b"%b %b" % (topic, message.encode()))
print("%s %s" % (topic, message))
i += 1
await trio.sleep(1)


async def sub():
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.bind("tcp://0.0.0.0:5556")
socket.setsockopt_string(zmq.SUBSCRIBE, '')
s = trzmq.Socket(socket)
while True:
string = await s.recv()
print(string)


async def run():
async with trio.open_nursery() as nursery:
nursery.start_soon(sub)
nursery.start_soon(pub, 1)
nursery.start_soon(pub, 2)


trio.run(run)
49 changes: 49 additions & 0 deletions examples/reqrep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import trio
import trzmq
import zmq

async def req():
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.SNDHWM, 2)
socket.connect("tcp://0.0.0.0:5556")

s = trzmq.Socket(socket)
i = 1
while True:
topic = b"ZMQ-Test"
message = f"Hello {i}".encode()
await s.send(b"%b %b" % (topic, message))
print("Sent: %s %s" % (topic, message))
reply = await s.recv()
print("Got response: %s" % reply)
i += 1
await trio.sleep(1)

async def rep():
await trio.sleep(10)

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.setsockopt(zmq.SNDHWM, 2)
socket.bind("tcp://0.0.0.0:5556")

s = trzmq.Socket(socket)
i = 1
while True:
msg = await s.recv()
await s.send(b'Reply to: ' + msg)

async def printer():
while True:
print("Still alive")
await trio.sleep(1)

async def run():
async with trio.open_nursery() as nursery:
nursery.start_soon(req)
nursery.start_soon(rep)
nursery.start_soon(printer)


trio.run(run)
17 changes: 17 additions & 0 deletions examples/sub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# subscriber

import trio
import trzmq
import zmq

async def run():
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.bind("tcp://0.0.0.0:5556")
socket.setsockopt_string(zmq.SUBSCRIBE, '')
s = trzmq.Socket(socket)
while True:
string = await s.recv()
print(string)

trio.run(run)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyzmq
trio
3 changes: 3 additions & 0 deletions trzmq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

from ._proxy import *
__all__ += _proxy.__all__

from ._socket import *
__all__ += _socket.__all__
Loading