[Python-ideas] Flagging blocking functions not to be used with asyncio

Guido van Rossum guido at python.org
Fri Oct 7 11:16:29 EDT 2016


On Fri, Oct 7, 2016 at 1:07 AM, Martin Teichmann
<lkb.teichmann at gmail.com> wrote:
> I am currently developing a Python library based on asyncio.
> Unfortunately, not all users of my library have much experience with
> asynchronous programming, so they often try to use blocking functions.
>
> I thought it would be a good idea if we could somehow flag blocking
> functions in the standard library, such that they issue a warning (or
> even raise an exception) if they are used in an asyncio context. For
> functions implemented in Python, a simple decorator should do the job.
>
> For functions implemented in C, things get a bit more complex.
> Thinking about it, I realized that currently the best indicator for a
> C function to block is that it releases the GIL. There are some false
> positives, like a read with O_NONBLOCK set, in which case we need a
> way to opt out, but in general it could be a good idea that releasing
> the GIL triggers a warning in an asyncio environment.

That implementation idea seems iffy -- it feels like it would be a lot
of work to pull it off. Releasing the GIL is done at an extremely low
level and it's not clear how you'd even raise an exception at that
point.

Maybe a simpler approach would be to write a linter that checks for a
known list of common blocking functions, and anything that calls those
automatically gets the same property?

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list