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

Jorropo . jorropo.pgm at gmail.com
Tue Mar 5 23:01:25 EST 2019

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

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 ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190306/046979f4/attachment.html>

More information about the Python-ideas mailing list