Skip to content

Running multiple callbacks asynchronously for a subscriber #555

Answered by charbonnierg
kafkaphoenix asked this question in Q&A
Discussion options

You must be logged in to vote

I'm not sure I understood your problem, but I think that you're looking for loop.run_in_executor() function to submit a blocking function within a coroutine function. Also, you need to have max_ack_pending greater than 1 if you want several callbacks to run at the same time.

Click to unfold an example
import time
import random
import asyncio

from concurrent.futures import ThreadPoolExecutor
from contextlib import AsyncExitStack
from typing import Callable, Coroutine

from nats.aio.client import Client as NATS
from nats.js import api as jetstream
from nats.js import errors as jetstream_errors
from nats.aio.msg import Msg


MsgCallback = Callable[[Msg], Coroutine[None, None, None]]
"""A t…

Replies: 5 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@kafkaphoenix
Comment options

Answer selected by kafkaphoenix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants