Skip to content

How to pass "contextvars" to tornado.concurrent.run_in_executor ? #2758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zhubijia opened this issue Oct 24, 2019 · 2 comments
Closed

How to pass "contextvars" to tornado.concurrent.run_in_executor ? #2758

zhubijia opened this issue Oct 24, 2019 · 2 comments

Comments

@zhubijia
Copy link

I build something with contextvars.Is there any way to pass it to tornado.concurrent.run_in_executor?

@zhubijia
Copy link
Author

zhubijia commented Oct 24, 2019

my code likes this:

from concurrent.futures import ThreadPoolExecutor
from tornado.ioloop import IOLoop
from tornado.concurrent import run_on_executor
import contextvars
ctx_var = contextvars.ContextVar("ctx_var")

class SleepHandler(tornado.web.RequestHandler):


    executor = ThreadPoolExecutor(10)


    @tornado.gen.coroutine
    def get(self):
        start = time.time()
        ctx_var.set('xxx')
        //how can i pass ctx_var to sleep
        res = yield self.sleep()
        self.write("when i sleep %f s" % (time.time() - start))
        self.finish()

    @run_on_executor
    def sleep(self):
        //here i want "ctx_var.get()"
        time.sleep(5)
        return 5

@bdarnell
Copy link
Member

It can't be done with the run_on_executor decorator (which is deprecated anyway). You can do it with contextvars.copy_context and IOLoop.run_in_executor:

    @tornado.gen.coroutine
    def get(self):
        start = time.time()
        ctx_var.set('xxx')
        ctx = contextvars.copy_context()
        res = yield IOLoop.current().run_in_executor(None, ctx.run, self.sleep)
        self.write("when i sleep %f s" % (time.time() - start))
        self.finish()

Full working example: https://repl.it/@bdarnell/PaltryStylishBooleanlogic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants