question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong order of callbacks

See original GitHub issue

Hello,

NATS documentation says, that messages from one publisher are received in the same order as they were published. But if we make few subscriptions, and publish messages for each one few times, callbacks will be called in different order. This is because each subscriber has own message queue. Here is example:

receive.py

import asyncio
from nats.aio.client import Client as NATS

async def run(loop):

    await nc.connect("nats://nats:4222", loop=loop)

    async def message_handler_A(msg):
        print('message_handler_A')

    async def message_handler_B(msg):
        print('message_handler_B')

    async def message_handler_C(msg):
        print('message_handler_C')

    await nc.subscribe("message_handler_A", cb=message_handler_A)
    await nc.subscribe("message_handler_B", cb=message_handler_B)
    await nc.subscribe("message_handler_C", cb=message_handler_C)
    print('receiving')


if __name__ == '__main__':
    nc = NATS()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(loop))
    loop.run_forever()

publish.py:

import asyncio
from nats.aio.client import Client as NATS

async def run(loop):
    await nc.connect("nats://nats:4222", loop=loop)
    for i in range(10):
      await nc.publish("message_handler_B", b"")
      await nc.publish("message_handler_C", b"")
      await nc.publish("message_handler_A", b"")

if __name__ == '__main__':
    nc = NATS()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(loop))
    loop.run_forever()

Just run publish.py after receive.py.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nxsofsyscommented, Sep 18, 2018

I’ve found that asyncio.Lock doing job. I added lock around callbacks which should run in ordered way.

0reactions
wallyqscommented, Sep 19, 2018

Closing as that is how the client is intended to work right now (similar behavior as in the Go client).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript callback - executing in wrong order - Stack Overflow
I am writing a script which is to upload files. I want to check if the file exists before the upload takes place,...
Read more >
[BUG] callbacks called in wrong order / too often #832 - GitHub
When I zoom in: update_limits callback should be called, which outputs to the limits store. update_data should now be called since it depends...
Read more >
What is the execution order of callbacks? - Dash Python
If no callbacks depend on each other, then the order is random and if you are running the app with multiple workers (e.g....
Read more >
Callback Execution - MATLAB & Simulink - MathWorks
The order in which listeners callback functions execute after the firing of an event is undefined. However, all listener callbacks execute synchronously ...
Read more >
Wrong order status (Cancelled) - WordPress.org
And since the callbacks are never processed, the orders are never marked as paid. Furthermore it sounds like your WooCommerce is cancelling unpaid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found