[Python-Dev] async __setitem__

562 views
Skip to first unread message

Martin Teichmann

unread,
Jun 19, 2015, 9:40:51 AM6/19/15
to pytho...@python.org
Hi everyone,

I was really happy to see PEP 492 - that is really a big step forward.
It makes many programming constructs possible in combination with
asyncio, which have been cumbersome or impossible until now.

One thing that still is impossible is to have __setitem__ be used
asynchronously. As an example, I am working with a database that
I access over the network, and it is easy to write

data = await table[key]

to get something out of the database. But getting something in, I have
to write something like

await table.set(key, value)

It would be cool if I could just write

await table[key] = value

which would, I think, fit in nicely with all of PEP 492.

That said, I was hesitating which keyword to use, await or async. PEP 492
says something like async is an adjective, await a command. Then for
setitem, technically, I would need an adverb, so "asyncly table[key] = value".
But I didn't want to introduce yet another keyword (two different ones for the
same thing are already confusing enough), so I arbitrarily chose await.

If we are really going for it (I don't see the use case yet, I just
mention it for
completeness), we could also be extend this to other points where variables
are set, e.g. "for await i in something():" which is a bit similar to
"async for i in something()" yet different.

Greetings

Martin
_______________________________________________
Python-Dev mailing list
Pytho...@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchive-30976%40googlegroups.com

Nick Coghlan

unread,
Jun 20, 2015, 9:56:38 AM6/20/15
to Martin Teichmann, pytho...@python.org
On 19 June 2015 at 22:56, Martin Teichmann <lkb.te...@gmail.com> wrote:
> to get something out of the database. But getting something in, I have
> to write something like
>
> await table.set(key, value)
>
> It would be cool if I could just write
>
> await table[key] = value

You've introduced an ambiguity here, though, as you're applying
async/await to a statement without a leading keyword.

In "await expr" it's clear "expr" is expected to produce an Awaitable,
and the await expression waits for it.

In "async for" and "async with" it's clearly a modifier on the
respective keyword, and hence serves as a good mnemonic for switching
to the asynchronous variants of the relevant protocols.

But what does an "asynchronous assignment" do? Do we need __asetattr__
and __asetitem__ protocols, and only allow it when the target is a
subscript operation or an attribute? What if we're assigning to
multiple targets, do the run in parallel? How is tuple unpacking
handled? How is augmented assignment handled?

If we allow asynchronous assignment, do we allow asynchronous deletion as well?

As you start working through some of those possible implications of
offering an asynchronous assignment syntax, the explicit method based
"await table.set(key, value)" construct may not look so bad after all
:)

Cheers,
Nick.

--
Nick Coghlan | ncog...@gmail.com | Brisbane, Australia

Yury Selivanov

unread,
Jun 20, 2015, 1:46:56 PM6/20/15
to pytho...@python.org
Hi Martin,

Actually, I think that it's better to adopt a bit different design:

async with db.transaction():
table[key1] = 'a'
table[key2] = 'b'

And then, in __aexit__ you should flush the updates to the database
in bulk. Usually, when working with an ORM, you need to update more
than one field anyways. And if it's just one,
'await table.set(key, val)' looks more obvious than
'await table[key] = val'.

I'd also recommend you to prohibit __setitem__ and __setattr__ outside
of a transaction.

Yury
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/yselivanov.ml%40gmail.com

Greg Ewing

unread,
Jun 20, 2015, 6:49:26 PM6/20/15
to pytho...@python.org
Nick Coghlan wrote:
> What if we're assigning to
> multiple targets, do the run in parallel? How is tuple unpacking
> handled? How is augmented assignment handled?
>
> If we allow asynchronous assignment, do we allow asynchronous deletion as well?

Yeah, we'd kind of be letting the camel's nose in
here. There's no obvious place to stop until *every*
dunder method has an async counterpart.

--
Greg

Guido van Rossum

unread,
Jun 21, 2015, 5:10:06 AM6/21/15
to Greg Ewing, pytho...@python.org
On Sun, Jun 21, 2015 at 12:48 AM, Greg Ewing <greg....@canterbury.ac.nz> wrote:
Nick Coghlan wrote:
What if we're assigning to
multiple targets, do the run in parallel? How is tuple unpacking
handled? How is augmented assignment handled?

If we allow asynchronous assignment, do we allow asynchronous deletion as well?

Yeah, we'd kind of be letting the camel's nose in
here. There's no obvious place to stop until *every*
dunder method has an async counterpart.

Exactly. We should not go here at all.
 
--
--Guido van Rossum (python.org/~guido)
Reply all
Reply to author
Forward
0 new messages