当我说锁定时,我的意思是它切换到其他协程并切换回来,直到完成某些工作。
代码应该这样写:
@waitUntil('myProcess')
@gen.coroutine
def query():
do_query()
@waitUntil('myProcess')
@gen.coroutine
def process():
result = yield do_myProcess(params)
deal_result(result)
这是我的 waitUntil,基于 toro.Lock 的答案。不保证正确,需要测试。
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
最佳答案
尝试toro.Lock ,我为此目的而写的
lock = toro.Lock()
@gen.coroutine
def f():
with (yield lock.acquire()):
assert lock.locked()
assert not lock.locked()
关于python - 有没有办法在 Tornado 中发送 "lock"请求,直到完成某些操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23841603/