sdk-for-python icon indicating copy to clipboard operation
sdk-for-python copied to clipboard

Idea: Make SDK asynchronous.

Open Niraj-Kamdar opened this issue 3 years ago • 9 comments

We can optimize the performance of the SDK by making it compatible to run in an asynchronous manner.
I have a few ideas about where to start. If this aligns with your current goal then I would be happy to contribute.

Niraj-Kamdar avatar Oct 03 '20 10:10 Niraj-Kamdar

Would be great if you could share your design idea in more detail, and what changes should be taken to make this feature possible.

eldadfux avatar Oct 03 '20 20:10 eldadfux

We can use aiohttp instead of requests to make client module async. By doing this we can make call method an awaitable coroutine and this will propagate all the way up to other classes like Database, Health, etc, and make it asynchronous.

What will be the benefits? Users can start multiple requests concurrently. Ex:

db = Database()
await db.create_collection(...) # req1
await db.create_collection(...) # req2

Here, both req1 and req2 will run concurrently instead of sequentially.

Niraj-Kamdar avatar Oct 04 '20 15:10 Niraj-Kamdar

@Niraj-Kamdar is there a way to make call method an awaitable natively without using any 3rd party dependency? Also what about backward compatibility after this change?

eldadfux avatar Oct 04 '20 18:10 eldadfux

Yes, we can make call method an awaitable by running it in threadpool but this solution has a con - by default, it will only run 5 tasks concurrently while native asynchronous library like aiohttp can run many tasks concurrently and all tasks run co-operatively instead of pre-emptively. For example, if I spawn 10 tasks concurrently when the underlying method is running on threads it will only run 5 tasks at a time, and when they complete it will start the remaining 5 tasks. In the case of the native async library like aiohttp, all tasks will spawn concurrently.

I believe concurrency is important to scale application but some people may still want a normal synchronous version of SDK maybe because their application hasn't designed to run concurrently. So, for backward compatibility, we have to consider the following options:

  • Create a separate async SDK.
  • Create async methods with "a" prefix in the same SDK. For example: call will be acall. Cpython has done this with __enter__ and __exit__ methods and they call it __aenter__ and __aexit__ when used asynchronously.

Niraj-Kamdar avatar Oct 05 '20 06:10 Niraj-Kamdar

@christyjacob4 is this relevant? Please close if not.

lohanidamodar avatar Feb 11 '22 05:02 lohanidamodar

Just my 5¢: might want to look into https://www.python-httpx.org/, which supports both sync and async, while still retaining Requests-like API. Not sure if it would be possible to not duplicate the wrapper, but at least it will be one external dependency vs two (requests + aiohttp).

notpushkin avatar Apr 11 '22 20:04 notpushkin

@notpushkin is this something you'd like to explore ?

christyjacob4 avatar Apr 12 '22 10:04 christyjacob4

This issue was opened 2 years ago & still not implemented. I highly support @Niraj-Kamdar 's idea. Please let us know if this is in the plan. And I would love to contribute if need any help.

eitozx avatar Sep 27 '22 14:09 eitozx

Latest PR on this: https://github.com/appwrite/sdk-generator/pull/453

stnguyen90 avatar Apr 28 '23 17:04 stnguyen90