Skip to content

FastStream publisher in syncronous code #854

Answered by Lancetnik
Jonas1312 asked this question in Q&A
Discussion options

You must be logged in to vote

@Jonas1312 this is a correct solution for making your broker syncronous. I'll add this wrapper to the framework in the future, but for now you can use it manually:

from contextlib import ExitStack

from anyio.from_thread import start_blocking_portal
from faststream.nats import NatsBroker

class Pubsliher:
    def __init__(self, broker: NatsBroker):
        self.broker = broker
        self.exit_stack = None
    
    def __enter__(self) -> "Pubsliher":
        return self.connect()

    def __exit__(self, *args, **kwargs):
        self.close()
    
    def connect(self):
        with ExitStack() as stack:
            portal = self.portal = stack.enter_context(start_blocking_portal())
     …

Replies: 12 comments 2 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
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
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

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

Comment options

You must be logged in to vote
0 replies
Comment options

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

Answer selected by Jonas1312
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
Converted from issue

This discussion was converted from issue #852 on October 16, 2023 17:31.