2

I am still new to greenlets and gevent, but from what I understand, greenlets yield to other greenlets either on an explicit yield statement, or on a blocking I/O operation. But are writes to stdout using a print statement blocking? And what about using the logging module to write to stdout, stderr, or a file on a local disk, or NFS?

Can a call to a print statement or a logging method cause a greenlet switch?

  • greenlet alone never yields implicitly. It's possible that gevent patches a number of standard library objects in order to get that effect (but I can't asnwer that, so this is only a comment, not an answer :-). – Armin Rigo Sep 05 '13 at 08:48
  • 1
    gevent does have a monkey patch operation that can replace certain modules with versions that yield to other greenlets on blocking IO calls (for example, gevent.socket). Then the thing I am wondering is, will gevent.monkey.patch_all replace any functions used by the logging module with versions that yield to other greenlets? – iHateCamelCase Sep 05 '13 at 16:29

1 Answers1

1

print statement is always blocking and will not switch to other greenlets. Avoid print instructions in any Gevent server code.

Newer Gevent versions have asynchronous file I/O gevent.os.tp_read that could be used with logging module to achieve non blocking operation. In this case log statements will cause Greenlet context switch.

Alex
  • 2,837
  • 5
  • 25
  • 27
  • This is surprising to me, because as far as I know blocking operations cause switches to other greenlets. – iHateCamelCase Oct 03 '13 at 01:51
  • @RelationRelatesItselfToItself async operations causes switch. The easiest way to find out is to experiment. time.sleep() is blocking function (without patching) and gevent.sleep() is asynchronous function. – Alex Oct 03 '13 at 18:54
  • Good info! You can also use get_hub().threadpool.spawn to run your logging statement, if you are not able to monkeypatch the logging module. This runs your function on gevent's default OS-threads pool. Very convenient. – jsalter Nov 10 '15 at 16:38