我目前遇到了一个问题,我在POST请求时使用线程在后台执行任务,但我在使用整数作为参数或任何数据类型作为参数时遇到了问题。
应该发生的是,它应该执行POST请求检查中的所有内容,然后在完成检查后执行其他操作。
导入:
from flask import Flask, url_for, render_template, redirect, request
from flask.cli import with_appcontext, click
import sqlalchemy
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_required, current_user线程类:
@click.command(name='run')
@click.pass_context
class Compute(Thread):
def __init__(self, secs):
Thread.__init__(self)
self.secs = secs
def run(self):
print('Thread Opened')
sleep(self.secs)
db.session.query(Users).filter(Users.username == current_user.username).first().requests += 1
db.session.commit()
print('Thread Closed')烧瓶路由:
@app.route('/index', methods=['GET', 'POST'])
@login_required
def index():
if request.method == 'POST':
time = '5'
thread = Compute(int(time))
thread.start()
return redirect(url_for('index'))
else:
return render_template('index.html')当线程尝试启动时,我收到的错误是:
Traceback (most recent call last):
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request()
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\flask_login\utils.py", line 272, in decorated_view return func(*args, **kwargs)
File "c:/Users/myuser/Desktop/Projects/Python/app.py", line 104, in layer4
thread = Compute(int(time))
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\click\core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "C:\Users\myuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\click\core.py", line 767, in main
args = list(args)
TypeError: 'int' object is not iterable我对线程不是很有经验,我只在其他项目中涉足过,所以我不确定为什么我会遇到这个错误;任何帮助我都非常感激。
发布于 2020-10-23 10:56:44
您需要删除@click.command和@click.pass_context装饰器。
@click.command(name='run')
@click.pass_context
class Compute(Thread):这些装饰器将类包装在一个函数中,该函数接受自己的参数。它用于从命令行运行函数和解析参数。这就是为什么回溯将args = list(args)显示为导致错误的行的原因。
https://github.com/pallets/click/blob/master/src/click/core.py#L916-L917
:param args: the arguments that should be used for parsing. If not
provided, ``sys.argv[1:]`` is used.https://stackoverflow.com/questions/64491447
复制相似问题