I'm trying to use python huey (https://github.com/coleifer/huey/blob/master/huey/api.py) to allow usage of a task queue with flask.
Starting with a function:
def some_long_calculation():
driver = create_chromedriver(True)
print(driver.capabilities['version'])
driver.get("https://www.yahoo.com/news/")
I import it into the tasks.py file and turn into into a task function with:
from . import my_huey
some_long_calculation_task = my_huey.task(some_long_calculation)
I then import some_long_calculation_task into main/views.py and try to run it with:
@main.route('/sanity_check')
def sanity_check():
png = some_long_calculation_task()
return send_file(io.BytesIO(png),mimetype='image/png')
but I get:
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>
How can I fix this?
Based on the error message, task
returns a decorator (usually used with @
as a syntax sugar).
Try this code instead:
from . import my_huey
some_long_calculation_task = my_huey.task()(some_long_calculation)
The "normal" usage from this would be something like
@my_huey.task()
def my_function(...):
...