[Python-ideas] Add hooks to asyncio lifecycle

Gustavo Carneiro gjcarneiro at gmail.com
Sat Jun 9 09:46:43 EDT 2018

On Sat, 9 Jun 2018 at 14:31, Michel Desmoulin <desmoulinmichel at gmail.com>

> Le 09/06/2018 à 12:33, Andrew Svetlov a écrit :
> > If we consistently apply the idea of hook for internal python structure
> > modification too many things should be changed. Import
> > machinery, tracemalloc, profilers/tracers, name it.
> > If your code (I still don't see the real-life example) wants to check a
> > policy change -- just do it.
> >
> > # on initialization
> > policy = asyncio.get_event_loop_policy()
> >
> > # somewhere in code
> > if policy is not asyncio.get_event_loop_policy():
> >     raise RuntimeError("Policy was changed")
> You need to repeat this check everywhere because nothing guaranty a code
> hasn't change it after the check.
> While a call back allow you to catch it every time.
> You can set either raise, warn, monkey patch, disable features, etc.

IMHO, it is not any framework's job to check for this.  It is a programmer
error.  You don't need to babysit programmers so much.  Not if the cost is
adding new APIs.

I agree with Andrew, if we open this precedent, next thing we know, Python
has to provide callbacks for any internal state changes.  Why not callbacks
for when modules are imported? Etc. etc.  It leads to API noise.  Doesn't
seem to be justified in this case, since I would guess almost all
applications only change event loop policy once, during startup, and never

Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180609/3d0a8f0b/attachment-0001.html>

More information about the Python-ideas mailing list