Live data from Hacker News

Python and Async Simplified (2018)

aeracode.org

1–10 of 47 posts

Re: Python and Async Simplified (2018)

#2
Can anyone recommend a good book/primer on "concurrency models" (is that a term?) for a self-taught programmer?

While I am self-taught, I'm used to (academic) books that strive for completeness. It is also what I prefer. Rather than something more pragmatic like a blog post.

It doesn't mean I want to read overly complicated prose on the subject, which I'm sure is possible.

Re: Python and Async Simplified (2018)

#3
post #2

Can anyone recommend a good book/primer on "concurrency models" (is that a term?) for a self-taught programmer? While I am self-taught, I'm used to (academic) books that strive for completeness. It is also what I prefer. Rather than something more pragmatic like a blog post. It doesn't mean I want to read overly complicated prose on the subject, which I'm sure is possible.

I don't have a book recommendation ready, but if you want to see how async can be used in a large codebase, have a look at the telethon [1] library. It's a python library for telegram and one of the few that actually implement MTProto. It's huge, generates a large chunk of its machinery automatically from the MTProto specs and is extremely (!) well structured.

This is much more useful than the typical "let's write a single-run example with async" blog post.

[1] https://github.com/LonamiWebs/Telethon

Re: Python and Async Simplified (2018)

#4
Similar question to the other one at the time of writing, but more specific: does anyone have a good, thorough introduction to the "async event loop" (sometimes known as "asyncio") pattern? By thorough I mean that it goes beyond a starter tutorial, into both examples of various supporting libraries and implementation details that matter for usage. I'm fine with a book, too.

There are popular libraries for it in both Python and Perl and I suspect I could make good use for it if I understood it.

Unfortunately, I've only ever used it in a cargo cult manner of sticking together functions until the error messages go away (yeah yeah, it was only for "throwaway" "prototypes") so I really don't understand how it all is meant to fit together.

Re: Python and Async Simplified (2018)

#5
It gives good pointers but it falls short on the usual suspects for an article on asyncio.

When teaching it, it's important to emphasis:

- await is locally blocking, so you should isolate linear workflows into their own coro, which is the unit of concurrency.

- to allow concurrency, you should use asyncio.create_task on coro (formerly ensure_future).

- you should always explicitly delimitate the life cycle of any task. Right now, this means using something like gather() or wait(). TaskGroup will help when it becomes mainstream.

A HN comment is not great to explain that, but if you read the article, you should investigate those points. There is no good asyncio code without them, only pain and disapointment.

Re: Python and Async Simplified (2018)

#6
post #2

Can anyone recommend a good book/primer on "concurrency models" (is that a term?) for a self-taught programmer? While I am self-taught, I'm used to (academic) books that strive for completeness. It is also what I prefer. Rather than something more pragmatic like a blog post. It doesn't mean I want to read overly complicated prose on the subject, which I'm sure is possible.

I think you should start by reading how "async" really works… that's call on a poll() (or epoll on linux) function, a loop, and a list of "call this function when this file descriptor can be written/read".

The whole async thing is there to abstract away and not have the program structured around the main loop… but in reality you have to keep in mind you are in a main loop that calls poll() and then all the registered functions.

Re: Python and Async Simplified (2018)

#7

It gives good pointers but it falls short on the usual suspects for an article on asyncio. When teaching it, it's important to emphasis: - await is locally blocking, so you should isolate linear workflows into their own coro, which is the unit of concurrency. - to allow concurrency, you should use asyncio.create_task on coro (formerly ensure_future). - you should always explicitly delimitate the life cycle of any tas…

> you should always explicitly delimitate the life cycle of any task

Unless you want a hacky actor system, in which case it's totally fine to `create_task` a ton of corountines which have their own spin loop with await sleep :)

Re: Python and Async Simplified (2018)

#9
post #2

Can anyone recommend a good book/primer on "concurrency models" (is that a term?) for a self-taught programmer? While I am self-taught, I'm used to (academic) books that strive for completeness. It is also what I prefer. Rather than something more pragmatic like a blog post. It doesn't mean I want to read overly complicated prose on the subject, which I'm sure is possible.

The book https://pragprog.com/titles/pb7con/seven-concurrency-models-... is actually pretty good, and much better than the title may suggest.

Re: Python and Async Simplified (2018)

#10

It gives good pointers but it falls short on the usual suspects for an article on asyncio. When teaching it, it's important to emphasis: - await is locally blocking, so you should isolate linear workflows into their own coro, which is the unit of concurrency. - to allow concurrency, you should use asyncio.create_task on coro (formerly ensure_future). - you should always explicitly delimitate the life cycle of any tas…

> you should always explicitly delimitate the life cycle of any task Unless you want a hacky actor system, in which case it's totally fine to `create_task` a ton of corountines which have their own spin loop with await sleep :)

One coroutine crashing and the others continuing to send it messages without noticing was my $40,000 bug.
Post reply on HN