Hi list,
first of all, I wanted to thank all the authors of asyncio. It's just
great, and makes my life as a programmer so much easier!
There is just one thing I have problems with: the naming of
the functions is, well, awkward. For long I thought that it's
just my poor English, but recently I have been introducing
asyncio to a coworker who is native english speaker, and
when I told him: if you want to run two coroutines ham and
spam in parallel, just write
await gather(ham(), spam())
the only result was laughter. Await gather? What should
that mean? I know what the authors meant, gather the futures,
but this is very theoretical, it's has nothing to do with what
one wants to do in the code.
The most weird of them is ensure_future. It used to be
called async, which made some sense to me. But telling
someone: if you want to run coroutine spam in the background,
write
await ensure_future(spam())
will also gain raised eyebrows at best. Sure, I can use
get_event_loop().create_task (finally a good name!), or
the Task constructor, but this makes the API very
non-systematic, why do we have sometimes functions
in the asyncio module, sometimes you have to call the
event loop?
run_in_executor also exists only in the event loop, and
has a weird calling syntax. Sure, it should not be used that
often, unfortunately I have to work with a C library that
exposes only a blocking API, so my code is full of lines
like
await get_event_loop().run_in_executor(None, spam())
which also doesn't look to nice to me.
Then there is a large overloading in the word wait:
await wait([ham(), spam())
await wait_for(spam(), timeout=3)
at least wait_for now has a similar context manager called
timeout, that is good wording.
So, I arbitrarily propose to introduce new names. Maybe
some native speaker comes up with even better names.
gather -> parallel
wait -> first_completed, first_exception and all_completed
wait_for -> timed
ensure_future -> create_task
and make run_in_executor also a function, but with the
first parameter put at the end.
Another naming problem is TimeoutError: it shadows
the builtin TimeoutError. This leads to awful bugs. As timeouts
often don't occur (and have a tendency that they lack
test cases, as no one likes long-running tests), once they
do occur, something bad happens. And even the typical
code cleaning utilities (like pyflakes) don't cry "you forgot
to import TimeoutError!" because it is built in. This is
why I propose to simply define TimeoutError to be the
builtin TimeoutError, or at least inherit from it.
Greetings
Martin