Skip to content
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

Can I write a single decorator combining @tornado.gen.coroutine and @run_on_executor. #3345

Closed
VARSHAJOSHY opened this issue Nov 8, 2023 · 2 comments

Comments

@VARSHAJOSHY
Copy link

VARSHAJOSHY commented Nov 8, 2023

I have written a code, and it was working well if my function doesnt have any yield statements, but not working when I was yielding the response as given below,

def tornadoWrapper(func):
      @run_on_executor
      def custom_executor(self, *args, **kwargs):
           return func(self, *args, **kwargs)

      @tornado.gen.coroutine
      def cutsom_gencoroutine(*args, **kwargs):
            yield custom_executor(*args, **kwargs)
class DummyHandlerWihourYield:
     @tornadoWrapper()
     def get(self):
          result = self.getResult()
class DummyHandlerWithYield:
     @tornadoWrapper()
     def get(self):
          result = yield self.getResult()
         
     def getResult(sef):
          #doing some processing

Also I would like to know (becoz I am bit confused on the concepts). Lets say I have hosted a webservice,

  1. if it doesnt have any 'yield', that means I have to support only multithreading not asynchronous - That means I need only @run_on_executor
  2. If it is yielding any result , which means I have to support both multithreading and asynchronous. I need both @run_on_executor and @tornado.gen.coroutine

Please correct me If I am wrong here.

@bdarnell
Copy link
Member

I don't think it's possible to do what you want here. Tornado is designed to be single-threaded, and if you try to automatically use threads you'll find that you can't do the kinds of things that a handler needs to do (for example, you're only allowed to call self.finish() from the event loop thread). When you're learning to use Tornado, I recommend not using threads at all, and only introducing threads after you have a solid grasp of asynchronous concurrency within a single thread.

@VARSHAJOSHY
Copy link
Author

VARSHAJOSHY commented Nov 16, 2023

I found the solution - simply remove the yield used in get()

def tornadoWrapper(func):
      @run_on_executor
      def custom_executor(self, *args, **kwargs):
           return func(self, *args, **kwargs)

      @tornado.gen.coroutine
      def cutsom_gencoroutine(*args, **kwargs):
            yield custom_executor(*args, **kwargs)
            
            
class DummyHandlerWithYield:
     @tornadoWrapper()
     def get(self):
          result = self.getResult()
         
     def getResult(sef):
          #doing some processing

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