gpt4 book ai didi

python - 我无法为 Flask/Celery 简单项目启动 Flower 监控服务器

转载 作者:行者123 更新时间:2023-11-30 22:37:43 30 4
gpt4 key购买 nike

我有一个基于 this tutorial 的简单 Flask/Celery 项目.

文件夹树如下:

ctest\  
- templates\
- index.html
- app.py
- __init__.py

index.html 文件是:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sum</title>
</head>
<body>
<h2>Sum a number with 100</h2>
{% for message in get_flashed_messages() %}
<p style="color: red;">{{ message }}</p>
{% endfor %}

<form method="POST">
<p>Use number: <input type="text" name="number" value="{{ number }}"></p>
<input type="submit" name="submit" value="Add">
<input type="submit" name="submit" value="Add in 30 secs">
</form>

</body>
</html>

app.py 是:

import os
from flask import Flask, request, render_template, session, flash, redirect, url_for
from celery import Celery
from celery.utils.log import get_task_logger

logger = get_task_logger(__name__)

# initialize Flask
app = Flask(__name__)

# Celery broker and backend configuration
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'

# Initialize extensions
app.config['SECRET_KEY'] = 'top-secret!'

# Initialize Celery
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'], backend=app.config['CELERY_RESULT_BACKEND'])
celery.conf.update(app.config)


@celery.task()
def make_async_sum(number):
with app.app_context():
logger.info("Executing....!")
return int(number) + 100

@app.route('/', methods=['GET','POST'])
def index():
# take the number from the form
if request.method == 'GET':
return render_template('index.html', number=session.get('number',''))
number = request.form['number']
session['number'] = number

if request.form['submit'] == 'Add':
# add now
make_async_sum.delay(number)
flash('Adding %s to 100'%number)
else:
# add later
make_async_sum.apply_async(args=[number], countdown=30)
flash('Adding %s to 100 , in 30 secs' % number)

return redirect( url_for('index') )

if __name__ == '__main__':
# use that host to be open from anywhere [cause I want to access it from outside the VM]
# use port 8181 [or smthing else] and not 8080
app.run(debug=True, host='0.0.0.0', port=8181)

它在 Vagrant VM 内运行。我可以从我的主机浏览器访问 index.html。我可以在虚拟机中通过 SSH 访问工作线程来查看正在使用 celery worker -A app.celery --loglevel=info 处理的任务。

但是,我无法启动该项目的 Flower 监控工具。

我尝试从 ctest\ 上方的目录路径中 flower -A ctest 但出现以下错误。

Unknown Celery version
Traceback (most recent call last):
File "/usr/local/bin/flower", line 9, in <module>
load_entry_point('flower==0.9.1', 'console_scripts', 'flower')()
File "/usr/local/lib/python2.7/dist-packages/flower/__main__.py", line 11, in main
flower.execute_from_commandline()
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 279, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 489, in setup_app_from_commandline
self._handle_user_preload_options(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 494, in _handle_user_preload_options
user_preload = tuple(self.app.user_options['preload'] or ())
AttributeError: 'Flask' object has no attribute 'user_options'

有什么想法吗?

最佳答案

看看那些:

  1. AttributeError: 'Flask' object has no attribute 'user_options'
  2. Starting celery in flask: AttributeError: 'Flask' object has no attribute 'user_options'

他们也有类似的问题(但 flower 没有)。

我会尝试按如下方式调用花朵:

celery flower -A ctest.app

celery flower -A ctest.app.celery

关于python - 我无法为 Flask/Celery 简单项目启动 Flower 监控服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804193/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com