Reputation: 4545
when i say lock ,i mean it switch to other coroutines and switch back until certain work is done.
the code would be write like these:
@waitUntil('myProcess')
@gen.coroutine
def query():
do_query()
@waitUntil('myProcess')
@gen.coroutine
def process():
result = yield do_myProcess(params)
deal_result(result)
here's my waitUntil based on the answer toro.Lock. don't guarantee it is right,need test.
import toro
_locks = {}
def getLock(key):
if key not in _locks:
_locks[key] = toro.Lock()
return _locks[key]
def waitUntil(key):
def wrapped(func):
@gen.coroutine
def wrapped2(*args,**kwargs):
with (yield getLock(key).acquire()):
result = yield func(*args,**kwargs)
return result
return wrapped2
return wrapped
Upvotes: 1
Views: 560
Reputation: 3068
Since v4.2, tornado has locks: https://www.tornadoweb.org/en/stable/locks.html
try locks.Lock:
>>> lock = locks.Lock()
>>>
>>> async def f():
... async with lock:
... # Do something holding the lock.
... pass
...
... # Now the lock is released.
see Lock documentation: https://www.tornadoweb.org/en/stable/locks.html#tornado.locks.Lock
Upvotes: 0
Reputation: 24007
Try toro.Lock, which I wrote for this purpose
lock = toro.Lock()
@gen.coroutine
def f():
with (yield lock.acquire()):
assert lock.locked()
assert not lock.locked()
https://toro.readthedocs.org/en/stable/classes.html#lock
Upvotes: 1