[Python-ideas] async/await in Python
Yury Selivanov
yselivanov.ml at gmail.com
Sun Apr 19 21:06:31 CEST 2015
Hi Antonie,
On 2015-04-19 2:52 PM, Antoine Pitrou wrote:
> On Sun, 19 Apr 2015 14:44:00 -0400
> Yury Selivanov <yselivanov.ml at gmail.com>
> wrote:
>> Queries in __getattr__
>> are bad because of many reasons.
> If you think they are bad, it means you probably don't like ORMs, since
> that's a primary feature of theirs.
I don't like that particular "feature" of most popular ORMs
available today.
We have a fairly advanced ORM developed in-house in my firm
that doesn't have this trait. (we are working hard to
open source it soon)
This is a slight off-topic, probably, but let me explain in
a greater detail:
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.
Now to modify something, you have to put your code
in 'with transaction()' block, otherwise it's an error
to write data.
At the end of 'with transaction()' we commit the modified
data.
Now, implementing 'with' statement was possible for us
only because we use greenlets, something that I don't
want to use.
Same goes for prefetching in 'for' loops -- only because
we use greenlets we can do that.
I'd really like to have some native syntax in python so
that I can do
posts = await BlogPost.select(...)
And later work with asynchronous iteration/context managers.
Thanks,
Yury
More information about the Python-ideas
mailing list