[Python-ideas] async/await in Python

Antoine Pitrou solipsis at pitrou.net
Sun Apr 19 21:23:11 CEST 2015


On Sun, 19 Apr 2015 15:06:31 -0400
Yury Selivanov <yselivanov.ml at gmail.com>
wrote:
> Hi Antonie,

(OT: It's Antoine, not "Antonie")

> In the bellow snippet of code we describe a shape of data
> that we want to fetch from the DB:
> 
> BP = sprymix.content.blog.BlogPost
> posts = BP.filter(BP.is_visible == True).select([
>          BP.title,
>          BP.label,
>          BP.publication_date,
>          BP.status,
>          BP.is_public,
>          (BP.header_image, [
>              BP.header_image.id
>          ]),
>          BP.body,
>          BP.is_owned,
>          (BP.owners, [
>              BP.owners.first_name,
>              BP.owners.last_name
>          ])
>       ])
> 
> Later, we can work with posts as with normal python object:
> iterate through it, access .title property and .owners
> collection etc.  Everything is fetched in one shot.

Powerful ORMs such as SQLAlchemy can let you decide, at mapping
definition time, which relations are fetched eagerly, which relations
are fetched lazily, etc. The important point is that regular code then
doesn't have to care: some attribute accesses run SQL queries, some
don't, and it all happens behind the scenes. This means you can pass
objects around without knowing up front which fields are going to be
needed by consuming code.

This "happens behind the scenes" aspect is what makes ORMs difficult to
mix with explicit multi-threading strategies such as "await" and "yield
from".

Regards

Antoine.




More information about the Python-ideas mailing list