[Python-ideas] Allow creation of polymorph function (async function executable syncronously)

Nathaniel Smith njs at pobox.com
Wed Mar 6 00:53:29 EST 2019


Defining a single polymorphic function is easy at the library level.
For example, with asyncio:

----

def maybe_async(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        coro = fn(*args, **kwargs)
        if asyncio.get_running_loop() is not None:
            return coro
        else:
            return await coro

@maybe_async
async def my_func(...):
    ... use asyncio freely in here ...

----

You can't do it at the language level though (e.g. with your proposed
'polymorph' keyword), because the language doesn't know whether an
event loop is running or not.

Extending this from a single function to a whole library API is
substantially more complex, because you have to wrap every function
and method, deal with __iter__ versus __aiter__, etc.

-n

On Tue, Mar 5, 2019 at 8:02 PM Jorropo . <jorropo.pgm at gmail.com> wrote:
>
> I was doing some async networking and I wondered, why I have to use 2 different api for making the same things in async or sync regime.
> Even if we make 2 perfectly identical api (except function will be sync and async), it will still 2 different code).
>
> So I first thinked to allow await in syncronous function but that create some problems (ex: an async function calling async.create_task) so if we allow that we have to asume to allways be in async regime (like js).
>
> Or we can differentiate async function wich can be awaited in syncronous regime, maybe with a new keyword (here I will use polymorph due to a lack of imagination but I find that one too long) ?
>
> So a polymorph function can be awaited in a syncronous function, and a polymorph function can only await polymorph functions.
>
> Polymorph function work exacly like async function BUT they assure of the ability to execute syncronously.
> And in a syncronous regime if an await need to wait (like async.sleep or network operation), just wait (like the equivalent of this function in syncronous way).
>
> So why made that ?
> To provide the same api for async and sync regime when its possible, example http api.
> This allow to code less librairy.
> Syncronous users can just use the librairy like any other sync lib (with the keyword await for executing but, personally, I think that is worth).
> And asyncronous users can run multiples tasks using the same lib.
> Moving from a regime to an other is simpler, source code size is reduced (doesn't need to create 2 api for the same lib), gain of time for the same reason.
>
> Also why it need to await in syncronous function, why not just execute polymorph function like any sync function while called in a sync function ?
> Because we need to create runnable objects for async.run, ...
>
> So I would have your though about that, what can be improved, a better name for polymorph ?
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the Python-ideas mailing list