4 ms·
I disagree that the code can't know what will switch with green threads. They switch on I/O and when explicitly requested. An example of using this to get rid o
by ntoshev 10y ago
I disagree that the code can't know what will switch with green threads. They switch on I/O and when explicitly requested. An example of using this to get rid of concurrency control: http://www.underengineering.com/2014/05/22/DIY-NoSql/ http://www.underengineering.com/2014/05/22/DIY-NoSql/
If you want to make sure that there are no context switching in certain part of your code, you can do it ASSERT-style, something like:
# enable this only if you use atomic, so a new module that should be imported before gevent
in_transaction = False
if __debug__:
import greenlet
old_switch=greenlet.greenlet.switch
class _greenlet(greenlet.greenlet):
def switch(*args, **kwargs):
if in_transaction:
raise Exception('Switching context during / atomic')
old_switch(*args, **kwargs)
setattr(greenlet, 'greenlet', _greenlet)
@contextmanager
def atomic():
"""
Ensure that a function or a block of code is atomic, raise exception if it's not
Usage:
@atomic()
def myTransaction(...):
...
or
with atomic():
...
"""
global in_transaction
in_transaction = True
yield
in_transaction = False
- plq 10y agoAnd how is this not a mutex?
- deleted 10y ago[deleted]
- ntoshev 10y agoThis only runs in the debug version of the code. I hope your mutexes run in production, too.
- lmm 10y agoSo you rely on hoping your tests exercise all possible paths, because if not you get silent race conditions in prod?
- ntoshev 10y agoIf you want you can run it in production, too (this would give you a warning, won't prevent race conditions, but there is almost no performance penalty). The point was this is very different from a mutex.