0

在此处输入图像描述

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

从一个函数开始:

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>

我怎样才能解决这个问题?

4

1 回答 1

2

根据错误消息,task返回一个装饰器(通常用作@语法糖)。

试试这个代码:

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

来自此的“正常”用法将类似于

@my_huey.task()
def my_function(...):
    ...
于 2018-08-22T03:37:17.863 回答