How to do non-blocking raw_input when using eventlet.monkey_patch () and why does it block everything even when executed in another thread?

I wrote this minimal code to explain my case:

import threading
import time
import eventlet

eventlet.monkey_patch()

def printing_function():
    while True:
        # here i want to do some work
        print "printing"
        time.sleep(1)

if __name__ == '__main__':
    thread = threading.Thread(target=printing_function)
    thread.start()

    while True:
        # here i want to wait for users input
        raw_input("raw input\n")
        print "inside main loop"
        time.sleep(1)

Even I have 2 threads, both of them are blocked when I call raw_input. When I comment on eventlet.monkey_patch (), only one thread is blocked and the other continues to print "print". Why and what should I do?

+2
source share
1 answer

I would say that there are several things:

  • raw_inputis not fixed eventlet, so its calls are blocked
  • threadingfixed eventlet, so threads act like coroutines

threading, . :

eventlet.monkey_patch()

:

eventlet.monkey_patch(os=True,
                     select=True,
                     socket=True,
                     thread=False,
                     time=True)

, thread True, : thread, threading, Queue.

: threading raw_input, :

def raw_input(message):
    sys.stdout.write(message)

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

sys.stdin, , . , eventlet, .

+2

All Articles