TypeError: decorator() missing 1 required positional argument: 'func'

TypeError: decorator() missing 1 required positional argument: 'func'

我正在尝试使用 python huey (https://github.com/coleifer/huey/blob/master/huey/api.py) 来允许使用带有 Flask 的任务队列。

从函数开始:

def some_long_calculation():
    driver = create_chromedriver(True)
    print(driver.capabilities['version'])
    driver.get("https://www.yahoo.com/news/")

我将它导入到 tasks.py 文件中并变成一个任务函数:

from . import my_huey
some_long_calculation_task = my_huey.task(some_long_calculation)

然后我将 some_long_calculation_task 导入 main/views.py 并尝试 运行 它:

@main.route('/sanity_check')
def sanity_check():
    png = some_long_calculation_task()
    return send_file(io.BytesIO(png),mimetype='image/png')

但我得到:

traceback (most recent call last):
  File "...lib\site-packages\flask\app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "...lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "...lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "...lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "...lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "...lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "...lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "...lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "...lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "...lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "...app\main\views.py", line 123, in sanity_check
    png = some_long_calculation_task()
TypeError: decorator() missing 1 required positional argument: 'func'

>>> some_long_calculation_task
<function Huey.task.<locals>.decorator at 0x0000028CF8CF2BF8>

我该如何解决这个问题?

根据错误信息,task returns装饰器(通常与@一起使用作为语法糖)。

试试这个代码:

from . import my_huey
some_long_calculation_task = my_huey.task()(some_long_calculation)

"normal" 的用法类似于

@my_huey.task()
def my_function(...):
    ...