Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:41:28+00:00 2026-06-14T07:41:28+00:00

I have the following problem. I work on a tornado based application server. Most

  • 0

I have the following problem.

I work on a tornado based application server. Most of the code can be synchronous and the web interface does not really use any of the asynchronous facilities of Tornado.

I now have to interface to an (asynchronous) legacy backend for which I use the tornado.iostream interface to send commands. Responses to these commands are sent asynchronously, together with other periodic information, such as status updates.

The code is wrapped in a common interface that is also used for other backends.

What I want to achieve is the following:

# this is executed on initialization
self.stream.read_until_close(self.close, self.read_from_backend)

# this is called whenever data arrives on the input stream
def read_from_backend(self, data):
     if data in pending:
         # it means we got a response to a request we sent out
         del self.pending[data]
     else:
         # do something else

# this sends a request to the backend
def send_to_backend(self, data):
     self.pending[data] = True
     while data in self.pending:
          # of course this does not work
          time.sleep(1)
     return 

Of course this does not work, as time.sleep(1) will not allow read_from_backend() to run any further.

How do I solve this? I want the send_to_backend() to return only when the response is received. Is there a way I can yield control to read_from_backend without yet returning from the method?

Please note that it is difficult to do this at a in the web layer using @asynchronous and @gen.engine, because that would require a full rewrite of all requests in our web layer. Is there a way I can implement the same design pattern somewhere else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:41:29+00:00Added an answer on June 14, 2026 at 7:41 am

    I think a good idea may be to look into using gevent. By MonkeyPatching and using a simple decorator I wrote you can get very easily nice asynchronous views which are written in a synchronous manner (blocking style).

    You can reuse most of the code from a previous answer of mine.

    Though you may not want to use gevent for different reasons (not having it has a dependency):

    Admitting that you’ve monkey patched your global process with :

    from gevent import monkey; monkey.patch_all()
    

    The above patches threads, sockets, sleep … so they go through gevent’s hub (the hub is to gevent what the ioloop is to tornado).

    Once patched and by using the @gasync decorator in my previous answer your view could simply be :

    class MyHandler(tornado.web.RequestHandler):
         @gasync
         def get(self):
             # Parse the input data in some fashion
             data = get_data_from_request()
    
             # This could be anything using python sockets, urllib ...
             backend_response = send_data_to_backend(data)
    
             # Write data to HTTP client
             self.write(backend_response)
    
             # You have to finish the response yourself since it's asynchronous
             self.finish()
    

    I find that gevent’s simplicity and “elegance” far outweighs any advantage you would have writing async code with tornado’s ioloop.

    In my case I had to use legacy code written in a synchronous fashion, so basically gevent was a life safer, all I had to do was monkey patch and write that decorator and I could use all that legacy code without any modifications.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following problem: i work in web application and only today. any
I have the following problem. I have a Seam web application which features e-mail
In my home work of Ackermann function I have solved the problem as following
I have following problem: I have built a tabbar application with 4 tabs. I
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have following problem. I want to create a file system based session storage
Hi I have the following problem and I am trying to work out what
I have the following problem: I have to write an application which makes modifications
i have following problem: i look for a variable to work with that comes
Hey guy i have following problem: I have this line of code which includes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.