On Thu, Jun 8, 2017 at 3:32 PM, manuel miranda <manu.mirandad@gmail.com> wrote:
Hello everyone,
After using asyncio for a while, I'm struggling to find information about how to support both synchronous and asynchronous use cases for the same library.
I.e. imagine you have a package for http requests and you want to give the user the choice to use a synchronous or an asynchronous interface. Right now the approach the community is following is creating separate libraries one for each version. This is far from ideal for several reasons, some I can think of:
- Code duplication, most of the functionality is the same in both libraries, only difference is the sync/async behaviors - Some new async libraries lack functionality compared to their sync siblings. Others will introduce bugs that the sync version already solved long ago, etc. - Different interfaces for the user for the same exact functionality.
In summary, in some cases it looks like reinventing the wheel. So now comes the question, is there any documentation, guide on what would be best practice supporting this kind of duality?
I would say that this is something that we as a community are still figuring out. I really like the Sans-IO approach, and it's a really valuable piece of the solution, but it doesn't solve the whole problem by itself - you still need to actually do I/O, and this means things like error handling and timeouts that aren't obviously a natural fit to the Sans-IO approach, and this means you may still have some tricky code that can end up duplicated. (Or maybe the Sans-IO approach can be extended to handle these things too?) There are active discussions happening in projects like urllib3 [1] and packaging [2] about what the best strategy to take is. And the options vary a lot depending on whether you need to support python 2 etc. If you figure out a good approach I think everyone would be interested to hear it :-) -n [1] https://github.com/shazow/urllib3/pull/1068#issuecomment-294422348 [2] Here's the same API implemented three different ways: Using deferreds: https://github.com/pypa/packaging/pull/87 "traditional" sans-IO: https://github.com/pypa/packaging/pull/88 Using the "effect" library: https://github.com/dstufft/packaging/pull/1 -- Nathaniel J. Smith -- https://vorpus.org