Flask 运行直接使用shell python中的uwsgidecorators的uWSGI应用程序

Flask 运行直接使用shell python中的uwsgidecorators的uWSGI应用程序,flask,uwsgi,pythoninterpreter,Flask,Uwsgi,Pythoninterpreter,您可能知道,uwsgidecorators仅在您的应用程序在uwsgi上下文中运行时才起作用,这在文档中并不清楚: 我的代码使用这些装饰器,例如用于锁定: @uwsgidecorators.lock def critical_func(): ... 当我使用uwsgi部署我的应用程序时,这很好,但是,当直接从Python shell启动它时,我得到了一个预期的错误: File ".../venv/lib/python3.6/site-packages/uwsgidecorators.py"

您可能知道,uwsgidecorators仅在您的应用程序在uwsgi上下文中运行时才起作用,这在文档中并不清楚:

我的代码使用这些装饰器,例如用于锁定:

@uwsgidecorators.lock
def critical_func():
  ...
当我使用uwsgi部署我的应用程序时,这很好,但是,当直接从Python shell启动它时,我得到了一个预期的错误:

File ".../venv/lib/python3.6/site-packages/uwsgidecorators.py", line 10, in <module>
  import uwsgi
ModuleNotFoundError: No module named 'uwsgi'

是否有任何已知的解决方案可以在两种模式下运行我的应用程序?显然,在使用简单解释器时,我不需要同步和其他功能来工作,但除了导入之外,做一些尝试似乎真的是很糟糕的编码。

在此期间,我做了以下实现,我很高兴知道有更简单的东西:

class _dummy_lock():
    """Implement the uwsgi lock decorator without actually doing any locking"""
    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kwargs):
        return self.f(*args, **kwargs)


class uwsgi_lock():
    """
    This is a lock decorator that wraps the uwsgi decorator to allow it to work outside of a uwsgi environment as well.
    ONLY STATIC METHODS can be locked using this functionality
    """
    def __init__(self, f):
        try:
            import uwsgidecorators
            self.lock = uwsgidecorators.lock(f)  # the real uwsgi lock class
        except ModuleNotFoundError:
            self.lock = _dummy_lock(f)

    def __call__(self, *args, **kwargs):
        return self.lock(*args, **kwargs)

@staticmethod
@uwsgi_lock
def critical_func():
  ...