bux
bux

Reputation: 7739

Simple PUB/SUB with aiozmq stream

I'm trying to make working a simple PUB/SUB with aiozmq stream (i don't want to use aiozmq rpc for some reasons) without success:

pub.py

# coding: utf-8
import asyncio
import time

import aiozmq
import zmq


async def do():
    stream = await aiozmq.stream.create_zmq_stream(
        zmq_type=zmq.PUB,
        bind='tcp://127.0.0.1:5556',
    )
    while True:
        await asyncio.sleep(1)
        msg = [str(time.time()).encode()]
        print('write ', msg)
        stream.write(msg)

loop = asyncio.get_event_loop()
loop.run_until_complete(do())

sub.py

# coding: utf-8
import asyncio

import aiozmq
import zmq


async def do():
    stream = await aiozmq.stream.create_zmq_stream(
        zmq_type=zmq.SUB,
        connect='tcp://127.0.0.1:5556',
    )
    while True:
        print('wait ...')
        msg = await stream.read()
        print('received ', msg)

loop = asyncio.get_event_loop()
loop.run_until_complete(do())

When execute pub.py:

python pub.py      
write  [b'1534927086.914483']
write  [b'1534927087.9154818']
write  [b'1534927088.9164672']

Then execute sub.py:

python sub.py
wait ...

What am i missing ?

Upvotes: 2

Views: 482

Answers (1)

bux
bux

Reputation: 7739

Simply miss a transport subscribe line in sub.py. There is a working sub.py:

# coding: utf-8
import asyncio

import aiozmq
import zmq


async def do():
    stream = await aiozmq.stream.create_zmq_stream(
        zmq_type=zmq.SUB,
        connect='tcp://127.0.0.1:5556',
    )
    stream.transport.subscribe(b'')

    while True:
        print('wait ...')
        msg = await stream.read()
        print('received ', msg)

loop = asyncio.get_event_loop()
loop.run_until_complete(do())

who produce:

python sub.py 
wait ...
received  [b'1534927504.0462704']
wait ...
received  [b'1534927505.0478334']

Upvotes: 2

Related Questions