OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

async version of Context.run for context vars in python asyncio?

  • Thread starter Thread starter Joseph Garvin
  • Start date Start date
J

Joseph Garvin

Guest
Python has the contextvars.copy_context() function which lets you store a copy of the current values of all the ContextVars that are currently active. Then later, you can run with all those values active by calling context_returned_by_copy_context.run(func). However, the run method is sync and expects a regular callable, not an async one. How can you run an async function with a context returned by copy_context?
<p>Python has the <a href="https://docs.python.org/3/library/contextvars.html#contextvars.copy_context" rel="nofollow noreferrer"><code>contextvars.copy_context()</code></a> function which lets you store a copy of the current values of all the <code>ContextVars</code> that are currently active. Then later, you can run with all those values active by calling <a href="https://docs.python.org/3/library/contextvars.html#contextvars.Context.run" rel="nofollow noreferrer"><code>context_returned_by_copy_context.run(func)</code></a>. However, the <code>run</code> method is sync and expects a regular callable, not an async one. How can you run an async function with a context returned by <code>copy_context</code>?</p>
 

Latest posts

Top