我想从Flask界面启动新的Redis工作者(作为新线程)。
为此,我具有以下功能(utils.py):
def start_worker():
listen = ['default']
redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
conn = redis.from_url(redis_url)
with Connection(conn):
print('STARTING WORKER..')
worker = Worker(Queue('default'), connection=conn, name='foo2')
worker.work()
和routes.py中的以下调用:
#from threading import Thread #import threading # for starting worker as new thread)
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=2)
@app.route('/')
@app.route('/index', methods=['GET','POST'])
@login_required
def index():
form = WorkerForm()
if form.validate_on_submit():
#w = Thread(target=utils.start_worker)
#w.daemon = True
#w.start()
executor.submit(utils.start_worker)
return redirect(url_for('index'))
现在,当我从控制台手动运行功能start_worker()
时,看到工作程序正在注册。
通过Flask调用函数时,我看到了抛出的注释(“ STARTING WORKER ..”)。但是没有工人在注册。
最初,我想以普通线程(带注释的代码)启动它,但这会导致ValueError: signal only works in main thread
。
我在这里可能想念什么? 谢谢