简体   繁体   中英

nats.io python client does not flush on close

I have a leetle test program which does not work unless I do a client.flush() before client.close() . A naive reading of nats/aio/client.py seems to suggest that close() does some kind of flush() , so I do not understand why my test program fails. Is it necessary to always flush() before close() ? Example programs don't seem to indicate so.

I can see that close() calls _flush_pending() , but this does something quite different from flush() :

  • flush() calls _send_ping() to send a PING and waits for a PONG response. _send_ping() writes directly to self._io_writer , and then calls _flush_pending() .

  • _flush_pending() pushes a None (anything will do, I guess) into self._flush_queue . This presumably wakes the _flusher() and causes it to write everything in self._pending to self._io_writer .

  • publish() calls _send_command() to push the messages onto self._pending , and then also calls _flush_pending() to cause the _flusher() to write everything.

The test program:

#!/usr/bin/env python3.5

import asyncio
import nats.aio.client
import nats.aio.errors

async def send_message(loop):
    mq_url = "nats://nats:password@127.0.0.1:4222"
    client = nats.aio.client.Client()
    await client.connect(io_loop=loop, servers=[mq_url])
    await client.publish("test_subject", "test1".encode())
    #await client.flush()
    await client.close()

def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(send_message(loop))
    loop.close()

if __name__ == '__main__':
    main()

FWIW if I send a large number of messages then, at some point (haven't yet worked out the exact conditions), messages are sent. We noticed the behaviour when processing a collection of files and sending a message for each line in the file: some files were making it through, others weren't, and it turned out it was the larger files (more lines) that made it. So it looks as though some internal buffer is filled and this forces a flush.

看起来可能是一个已修复的错误: https : //github.com/nats-io/asyncio-nats/pull/35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM