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 8057917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:11:14+00:00 2026-06-05T09:11:14+00:00

I’ve written a simple tcp server using gevent.StreamServer for testing purposes. In order for

  • 0

I’ve written a simple tcp server using gevent.StreamServer for testing purposes. In order for me to send responses to some of the clients, I need a non-blocking way to handle input via raw_input(), preferrably without using threads.

After some googling I stumbled across this question: How to make non-blocking raw_input when using eventlet.monkey_patch() and why it block everything, even when executed on another thread?

I’ve written the following and it does exactly what I want however I assume there’s better approach to it. Could someone point me to the right direction? Also, an idea why the try/except does not catch the KeyboardInterrupt is appreciated.

import select
from gevent.monkey import patch_all
from gevent.server import StreamServer

patch_all(os=True, select=True)

def raw_input(message):
    """ Non-blocking input from stdin. """
    sys.stdout.write(message)

    select.select([sys.stdin], [], [])
    return sys.stdin.readline()

def main():
    """ Run the server, listen for commands """

    server = StreamServer(("0.0.0.0", 6000), handle)
    print "Starting server"
    gevent.signal(signal.SIGTERM, server.close)
    gevent.signal(signal.SIGQUIT, server.close)
    gevent.signal(signal.SIGINT, server.close)

    server.start()
    while True:
        try:
            a = raw_input("")
            if a:
                print "Received %s" % a
            gevent.sleep(0)
        except KeyboardInterrupt:
            print "Received a shutdown signal, going down ..."
            server.stop()
            sys.exit(0)

if __name__ == "__main__":
    main()

EDIT: I’ve rewritten parts of the code and I now understand the silliness of the main()-function. I’ll post it as an edit in case someone stumbles on this question and has a better idea on how to do it.

from gevent.signal import signal

def get_console_input():
    """ Non-blocking console input to the server """

    select.select([sys.stdin], [], [])
    # There's quite a bit of code here but it is input handling so 
    # for shortness's sake   I've snipped everything and return the line read.
    return sys.stdin.readline()

def exit(server):
    """ Quit the server gracefully """

    print "Received shutdown signal, going down. """
    server.close()
    sys.exit(0)

def main():
    """ The main function. Create and run the server, listen for commands and
   append any command to the server so it can send them on to the clients """

    # Myserver is a class which inherits from gevent.server.StreamServer.
    # Myserver could just as well be replaced by gevent.server.StreamServer.

    server = MyServer(("0.0.0.0", PORT))
    print "Starting server"

    # Add some signal handlers to quit the server gracefully.
    gevent.signal(signal.SIGTERM, exit, server)
    gevent.signal(signal.SIGQUIT, exit, server)
    gevent.signal(signal.SIGINT, exit, server)

    # Server started in async mode
    server.start()

    while True:
        get_console_input()
        gevent.sleep(0)

if __name__ == "__main__":
    main()
  • 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-05T09:11:16+00:00Added an answer on June 5, 2026 at 9:11 am

    The simplest way I’ve found to do this is to use gevent.socket.wait_read to wait until sys.stdin can be read:

    wait_read(sys.stdin.fileno())
    return sys.stdin.readline()
    

    I’ve also written a small wrapper around file descriptors to give them non-blocking .read() and .write() methods: https://gist.github.com/2915875

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.