Python 从线程中删除会话变量
我尝试实行投票制。它是这样工作的。如果用户投票,我会将其临时状态记录在会话变量中:upvoted、starred等 如果当前用户在我将结果保存到临时表之前没有投票。用户可在5分钟内更改投票。5分钟后,通过线程将结果永久写入数据库 我想在结果写入数据库后清除临时会话变量。有没有办法做到这一点 task.pyPython 从线程中删除会话变量,python,multithreading,python-3.x,flask,Python,Multithreading,Python 3.x,Flask,我尝试实行投票制。它是这样工作的。如果用户投票,我会将其临时状态记录在会话变量中:upvoted、starred等 如果当前用户在我将结果保存到临时表之前没有投票。用户可在5分钟内更改投票。5分钟后,通过线程将结果永久写入数据库 我想在结果写入数据库后清除临时会话变量。有没有办法做到这一点 task.py import threading import time from flask import Flask, copy_current_request_context, session from
import threading
import time
from flask import Flask, copy_current_request_context, session
from threading import Lock
from vote import voteQuestion
app = Flask(__name__)
app.secret_key="appkey"
@app.before_first_request
def initializeTask():
@copy_current_request_context
def runTask():
while True:
voteQuestion()
time.sleep(10)
task = threading.Thread(target=runTask)
task.start()
@app.route('/vote')
def vote():
session['test'] = "This is a test"
return 'success'
@app.route("/")
def hello():
return "Hello world!"
if __name__ == "__main__":
app.run()
投票.py
from flask import session
def voteQuestion():
print('session variables', session.items())
result = session.get('test', 'not set')
print ('result ', result)
if 'test' in session:
session.pop('test', None)
print ('Running in the background')
不,不可能。请求结束后,该线程中的会话本质上是只读副本。向它写入不会有任何作用,因为没有响应将更新的cookie传送到浏览器 在存储临时投票时,将时间戳存储在临时表中会更有意义,而不是尝试对线程和会话执行某些操作