0

The situation I have in mind is using a python extension module that can call back into python, so that there may be a mixture of python and non-python stack frames at the point when a greenlet yields.

I assume that if a module uses thread-local storage it is likely to misbehave with greenlets.

Is there any other reason why a thread-safe module might not be greenlet-safe?

Edit: what I really want to know is whether there is any difference between the way context switches are implemented for greenlets vs. regular threads. Do greenlets take any shortcuts that might work for python but break for some kinds of extension module?

Oliver Goodman
  • 671
  • 6
  • 14
  • It depends what you're doing with thread-local storage. If it's actually *request*-local, yes, you'll have a problem. If it's a stateful object that will be reset before switching greenlets, you're fine. – David Ehrmann Jun 26 '15 at 00:53

1 Answers1

1

Greenlets stay within a single thread in Python. They cannot jump into another thread. So if your code is properly thread-safe, it will be greenlet safe.

Another way to look at it is that greenlets execute only one at a time, so you have very little issues like you have with threads.

DevShark
  • 8,558
  • 9
  • 32
  • 56
  • At least for thread safety, the difference is greenlits context switch on blocking IO, sleeps, etc. Threads context switch whenever. – David Ehrmann Jun 26 '15 at 00:51