Context Variables should be created at the top module level and never in closures

19 Views Asked by At

In the documentation for the Python standard library module contextvars, it is stated that:

Context Variables should be created at the top module level and never within closures.

However, I am unclear about the meaning of this statement. While many online tutorials mention it, none provide an explanation. Could you please provide an example where a context variable is created within a closure, and explain the potential problems it may cause? Is it related to GC?

Context objects hold strong references to context variables which prevents context variables from being properly garbage collected.

0

There are 0 best solutions below