airtai / faststream

FastStream is a powerful and easy-to-use Python framework for building asynchronous services interacting with event streams such as Apache Kafka, RabbitMQ, NATS and Redis.
https://faststream.airt.ai/latest/
Apache License 2.0
2.45k stars 124 forks source link

Feature: broker syncify wrapper #1289

Closed allran closed 6 months ago

allran commented 6 months ago

To suggest an idea or inquire about a new Message Broker supporting feature or any other enhancement, please follow this template:

Is your feature request related to a problem? Please describe. Provide a clear and concise description of the problem you've encountered. For example: "I'm always frustrated when..."

Describe the solution you'd like Clearly and concisely describe the desired outcome or solution.

Feature code example To help others understand the proposed feature, illustrate it with a FastStream code example:

from faststream import FastStream
...

Describe alternatives you've considered Provide a clear and concise description of any alternative solutions or features you've thought about.

Additional context Include any other relevant context or screenshots related to the feature request.

Lancetnik commented 6 months ago

We were thinking about it, but consider to support only async API Is it problem for you?

allran commented 6 months ago

Because when sending a message, I need the following encapsulation to work properly:

def publish_msg(*args, **kwargs):
    """
    发送消息
    """
    async def send_msg():
        #
        broker_pub = kwargs.get('broker', None)  # 是否有传入broker
        if broker_pub is None and len(args) > 0:
            bro = args[0]
            if isinstance(bro, RabbitBroker):  # 传入的第一个参数,是否是broker
                broker_pub = bro
        if broker_pub is None:
            async with RabbitBroker(broker_url) as broker_pub:
                await broker_pub.publish(*args, **kwargs)
        else:
            await broker_pub.connect()
            await broker_pub.publish(*args, **kwargs)

    asyncio.run(send_msg())

if __name__ == "__main__":
    publish_msg("messageOne", "in")
Lancetnik commented 6 months ago

I have a plan to add syncify class from this discussion. Now you can use it by youself

allran commented 6 months ago

ok, thanks very much. @Lancetnik