-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asyncio-based Python Client #5939
Comments
No, I could not use swagger client in async mode. |
Have you tried making the async call by providing the |
@wing328 that does indeed spawn a thread and call a callback, which is a form of asynchronous programming, but I was referring to the python3 asyncio. It is possible to integrate synchronous http libraries into asyncio via the future module (https://pymotw.com/3/asyncio/executors.html), but it still requires native threads to execute, which is undesirable due to limitations of system-thread based programming which asyncio is designed to overcome in the first place. It sounds like it's worth giving the implementation a shot. thanks! |
@toumorokoshi thanks for providing more info. Shall we use a HTTP lib that supports Python3 asyncio? What about aiohttp? I've not tried it myself but based on the example it looks like it may be suitable for our use case. Other languages such as Java supports more than 1 HTTP library via a |
asyncio (and aiohttp in particular) would be a neat feature to have! I would, however, not bother with Python <3.5 support. If one needs the older Python versions support, there will be a "classic" [synchronous] generator mode, thus the clients provider would publish two clients (client-classic, client-asyncio). |
Just to share a bit more. For JS, we recently introduced the "useES6" option to allow JS generator output codes that are no longer compatible with ES5. For Python Flask generator, there's the option "supportPython2" to make the output compatible with Python 2.x For |
Well, asyncio is still not as widely applied to make it the default for Python 3.5. Synchronous APIs and even APIs with callbacks are still easier to understand and implement than asyncio in Python. If your project is not written in asyncio, than the API calls would look like this: users_api = my_client.UsersApi()
import asyncio
loop = asyncio.get_event_loop()
users = loop.run_until_complete(users_api.get_users()) |
I'd like to clarify my approach, because I think these are all valid concerns. I'd like to create a new module for async based apis:
I'm have a branch I'm working on now, with my implementation. Going a little slowly due to time, but it's not terribly difficult work: https://github.com/toumorokoshi/swagger-codegen Details: @wing328 since we don't execute modules in python unless we load them, I can just write a python3 specific module that will raise an exception when it gets loaded by python 2. I'm hoping to write a 2-3 compatible library to make it easier for the client author. Due to the requirement that asyncio uses coroutines throughout the whole call chain, I can't do something that is arguably better like:
I wish Python had designed it that way, it makes a lot of code way easier to write. |
@toumorokoshi I would suggest you to open a PR with the enhancement so that others can more easily review and test the change. |
@toumorokoshi btw, not sure if you've noticed. The Python API client in the 2.3.0 branch has been refactored (e.g. removed singleton) so I would suggest you to take a look and base the enhancements on that. |
@toumorokoshi I don't see much point in having 2 copies of the client generated into a single module. Also, you will end up with a copy of the templates with just |
@wing328 sure, I'll start one up when I have a working sketch. @frol I was thinking from the perspective of someone publishing the library, it might nice to have them in the same codebase, similar to Motor's support of Tornado and Asyncio: https://github.com/mongodb/motor. But, it's not a big deal either way. I'll can add an option for asyncio. |
@toumorokoshi My thinking targets the consumers. I haven't seen a case where the need to use a module via different APIs in a single project would make sense. Given this fact, having two sets of APIs in a single client just bloats the installation. |
I would suggest going with the easiest implementation as a starting point
and it will get better and better based on feedbacks and contributions from
the community.
…On Jul 5, 2017 3:23 AM, "Vlad Frolov" ***@***.***> wrote:
@toumorokoshi <https://github.com/toumorokoshi> My thinking targets the
consumers. I haven't seen a case where the need to use a module via
different APIs in a single project would make sense. Given this fact,
having two sets of APIs in a single client just bloats the installation.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5939 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AA5BdM-aATP3J3hwtww1QNtI_HPsjYtfks5sKpEqgaJpZM4OHhg9>
.
|
sounds good. I'll do a PR when I have a feature flag version ready. |
Since I haven't had my hands on AsyncIO, I believed you, but it seems that this statement is false, at least in Python 3.6: import asyncio
async def qq(a):
await asyncio.sleep(2)
return 1
def qq2():
return qq(1)
async def qq3():
return await qq2()
l = asyncio.get_event_loop()
l.run_until_complete(qq3()) This code works just fine for me. Notice that P.S. The suggestion about a separate package for AsyncIO is a bit awkward and complicates maintenance, but it still seems to be a valid approach. |
Description
I'm interested in adding code into the existing python client to a native asyncio client for swagger.
https://docs.python.org/3/library/asyncio.html
Does this already exist? Or should I talk to anyone before attempting this?
My General approach was going to be:
@masterandrey I see you're using async with the existing swagger client: do you have any comments?
Command line used for generation
-l python
The text was updated successfully, but these errors were encountered: