Hi Dima, 2017-12-13 7:39 GMT+01:00 Dima Tisnek <dim...@gmail.com>: > get()/set(value)/delete() methods: Python provides syntax sugar for > these, let's use it. > (dict: d["k"]/d["k] = value/del d["k"]; attrs: obj.k/obj.k = value/del > obj.k; inheriting threading.Local)
I was trapped by Context which is described as "a mapping". Usually, when I read "mappin", I associate it to a mutable dictionary. But in fact Context is a *read-only* mapping. Yury changed the Introduction to add "read-only", but not the Context section: https://www.python.org/dev/peps/pep-0567/#contextvars-context Only a single ContextVar variable can be modified. This object is a container for a *single* value, not a mapping, you cannot write "var = value", you have to write "var.set(value)", and "var['key] = value" doesn't make sense. > This PEP and 550 describe why TLS is inadequate, but don't seem to > specify how proposed context behaves in async world. I'd be most > interested in how it appears to work to the user of the new library. In short, context is inherited automatically, you have nothing to do :-) Put anything you want into a context, and it will follow transparently your asynchronous code. The answer is in the sentence: "Tasks in asyncio need to maintain their own context that they inherit from the point they were created at. " You may want to use a task context to pass data from a HTTP request: user name, cookie, IP address, etc. If you save data into the "current context", in practice, the context is inherited by tasks and callbacks, and so even if your code is made of multiple tasks, you still "inherit" the context as expected. Only tasks have to manually "save/restore" the context, since only tasks use "await" in their code, not callbacks called by call_soon() & cie. > token is fragile, I believe PEP should propose a working context > manager instead. Why is it fragile? In asyncio, you cannot use a context manager because of the design of tasks. Victor _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com