langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
45.33k stars 6.37k forks source link

Request for Asynchronous Interface in Python Client #6285

Closed imgaojun closed 1 week ago

imgaojun commented 2 months ago

Self Checks

1. Is this request related to a challenge you're experiencing? Tell me about your story.

Hello Dify team,

I'm a Python developer and a user of your API. I really appreciate the work you've done in providing a Python client for interacting with your services. However, I've noticed that the current implementation lacks support for asynchronous operations, which can be beneficial in certain scenarios, especially when dealing with long-running tasks or high concurrency.

I would like to request the addition of an asynchronous interface to your Python client. This could be implemented as a separate class or as an optional mode within the existing client. The asynchronous interface should support all the existing methods, allowing developers to make non-blocking requests and handle responses asynchronously.

2. Additional context or comments

No response

3. Can you help us with this feature?

StudyingLover commented 1 month ago

Do you mean async? I think you can send a http req by this way

import aiohttp

url = your_url
data = your_data

class ClientSessionSingleton:
    _session = None

    @classmethod
    def get_session(cls):
        if cls._session is None or cls._session.closed:
            cls._session = aiohttp.ClientSession()
        return cls._session

    @classmethod
    async def close_session(cls):
        if cls._session is not None:
            await cls._session.close()
            cls._session = None

def session():
    return ClientSessionSingleton.get_session()

async with session().post(url, data=data) as resp:
    try:
            response = await resp.json()
        except Exception as error:
            pass
    # do some thing