python - 有没有办法在 Tornado 中发送 "lock"请求,直到完成某些操作?

标签 python tornado

当我说锁定时,我的意思是它切换到其他协程并切换回来,直到完成某些工作。

代码应该这样写:

@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()

https://toro.readthedocs.org/en/stable/classes.html#lock

关于python - 有没有办法在 Tornado 中发送 "lock"请求,直到完成某些操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23841603/

相关文章:

python - 设置 pandas 数据框的格式

python - 使用 Tornado 和 SUDS 的异步 SOAP 调用

python - 如何将 "merge"两个Python扭曲应用程序?

python - Tornado .web : Is there a method which is called after the actual handler method?

python - Tornado :将更多参数传递给响应回调

python - 格式化字典打印输出

python - 如何让 Python 的 subprocess() 与 input() 交互?

Python - 如何在完成写入后移动文件

python - python 3.7 中 .append 函数的自动化

python - gevent + gunicorn 是否可扩展且稳定用于生产?