I tried python curio.
The basic idea is to use async / await. In other words, it becomes "create a task and turn it". Tasks are declared in the form of functions with the ʻasync keyword. In the interactive shell, you will prepare and execute something that "turns the task". There has been ʻasyncio
for a long time, but it can be curio
. Alternatively, you may call the coroutine method.
>>> async def hello():
... return "Hello"
...
>>> import curio
>>> curio.run(hello())
'Hello'
>>> import asyncio
>>> asyncio.get_event_loop().run_until_complete(hello())
'Hello'
>>> try:
... hello().send(None)
... except StopIteration as e:
... e.value
...
'Hello'
curio
can be short.
When this happens, it becomes a comparison between ʻasyncio and
curio. ʻAsyncio
doesn't seem to be the best design for async / await, partly because it was before the async / await syntax was created (https://vorpus.org/blog/some-thoughts-on- asynchronous-api-design-in-a-post-asyncawait-world /). As you get used to curio
, ʻasyncio feels like a highway without guardrails. I hesitate to say [it will be faster](https://magic.io/blog/uvloop-blazing-fast-python-networking/) in ʻuvloop
.
asyncio author. https://www.youtube.com/watch?v=m28fiN9y_r8
curio author. Live coding is vivid https://www.youtube.com/watch?v=ZzfHjytDceU https://www.youtube.com/watch?v=MCs5OvhV9S4
On the other hand, there are quite a few quirks in async / await itself. The standard library implicitly uses the socket
object inside, but when used normally, I / O goes into blocking mode, which makes it difficult to combine. Apart from the standard library, there is also a library that has socket
removed and can be used as a state machine by focusing on the protocol part. For example, h11
and hyper h2
are relatively easy to combine in async / await.
h2 author. https://www.youtube.com/watch?v=7cC3_jGwl_U
If you try a few things in this direction, you'll get something like gevent
, ʻeventlet without
monkey_patch. Compared to
curio`, does it feel like weighing the terrible side effects and convenience of monkey_patch?
The problem until the very end is the database connection. I'm still looking for a use case to use SQLAlchemy ... It is difficult to create a good protocol parser with a pattern that seeks around large files with structure.
By the way, the developer documentation says Please, Don't Use Curio!. I laughed. Well ... if gevent implements the PEP492 __await__
, it would be a gold rod for demons. I'm not confident that asyncio won't cause an accident.
It's nice to be able to detect await leaks when using async / await.
if __name__=="__main__":
import logging
logging.getLogger("asyncio").setLevel(logging.DEBUG)
import gc
gc.set_debug(gc.DEBUG_UNCOLLECTABLE)
loop = asyncio.get_event_loop()
loop.set_debug(True)
try:
loop.run_forever()
finally:
loop.close()
Recommended Posts