コード例 #1
0
async def main(nsq: NSQConnection):
    await nsq.subscribe("test_topic", "channel1", 2)
    while True:
        async for message in nsq.messages():
            print("Message: " + str(message))
            # message.body is bytes,
            # __str__ method decodes bytes
            #
            # Something do with messages...
            # Then, mark as processed it
            await message.fin()

        # If you doesn't break the loop
        # and doesn't set auth_reconnect parameter to False,
        # but you have reached this point,
        # it's means that the NSQ connection is lost
        # and cannot reconnect.
        #
        # Errors in ansq package are logging with ERROR level,
        # so you can see errors in console.
        print("Connection status: " + str(nsq.status))
        # Prints one of this:
        # Connection status: ConnectionStatus.CLOSING
        # Connection status: ConnectionStatus.CLOSED

        # You can reconnect here in try-except block
        # or just leave the function and finish the program.
        # It's all depends on the design of your application.
        return
コード例 #2
0
async def test_command_sub():
    nsq = NSQConnection()
    await nsq.connect()
    assert nsq.status.is_connected

    response = await nsq.sub('test_topic', 'channel1')
    assert response.is_ok

    await nsq.close()
    assert nsq.is_closed
コード例 #3
0
async def test_command_without_connection():
    nsq = NSQConnection()
    assert nsq.status.is_init

    with pytest.raises(AssertionError) as e:
        await nsq.pub('test_topic', 'test_message')
    assert str(e.value) == 'You should call `connect` method first'

    await nsq.close()
    assert nsq.status.is_init
コード例 #4
0
async def test_command_without_identity():
    nsq = NSQConnection()
    await nsq.connect()
    assert nsq.status.is_connected

    response = await nsq.pub('test_topic', 'test_message')
    assert response.is_ok

    await nsq.close()
    assert nsq.is_closed
コード例 #5
0
async def test_command_without_connection():
    nsq = NSQConnection()
    assert nsq.status.is_init

    with pytest.raises(
            AssertionError,
            match="^You should call `connect` method first$",
    ):
        await nsq.pub("test_topic", "test_message")

    await nsq.close()
    assert nsq.status.is_init