aiotask-context icon indicating copy to clipboard operation
aiotask-context copied to clipboard

Task factory used in example

Open nikhilk opened this issue 5 years ago • 1 comments

Is the simple task_factory used in the PoC example the best choice?

It seems you'd want the request id to propagate to child tasks created within request processing. So one of the other two task factories seem more appropriate. Likely the copying one.

Is that correct understanding? I wonder if the example should be updated.

Side note -- this functionality is super useful, and should be more deeply merged into asyncio IMO.

nikhilk avatar Jul 07 '18 23:07 nikhilk

Hey @nikhilk sorry for the delay, been on annual leave for couple of months :).

With the simple task factory, a dict is used and the dict is propagated from parent to child. A simple child_context = parent_context is done. This has the side effect that if you update an already existing key in the child, the parent will also be affected because it is the same object. If you don't want this behavior, you can set the copy_context variable to True and then deepcopy will be use (with the extra overhead this implies).

Side note -- this functionality is super useful, and should be more deeply merged into asyncio IMO.

Glad you like it, it's proven very useful for us and it is really simple to use. In Python 3.7 you can use contextvars module (pep-567)

argaen avatar Sep 10 '18 14:09 argaen