1

Why does eventlet allow it to finish the 6 seconds, when it should exit the indentation after 5 seconds?

>>> with eventlet.Timeout(5):
        time.sleep(6)
        x = 1
>>> x
1
User
  • 23,729
  • 38
  • 124
  • 207
  • 1
    I'm pretty sure you're supposed to use `eventlet.sleep` if you want an eventlet greenthread to sleep. – user2357112 Jun 12 '15 at 04:51
  • @user2357112 is this something particular to sleep or if i use other calls, it also won't exit them halfway through? – User Jun 12 '15 at 15:07

1 Answers1

3

Eventlet provides cooperative multithreading. Which means you need to yield control to give hub or coroutines (in this case, hub implements timeouts) chance to run. To yield control:

  • either use green versions of IO and sleep
  • or execute eventlet.monkey_patch(), now you can use regular time, socket, etc modules, replaced by "green" versions, cooperating with Eventlet.

Any CPU tight code without green calls, for example [_ for _ in xrange(1000000000)] is impossible to interrupt at all. If you find yourself in similar situation, place eventlet.sleep(0) somewhere in loop, that would enter Eventlet hub and allow timeouts to work.

temoto
  • 5,394
  • 3
  • 34
  • 50