Python Flask在新请求后更新var



我用Python创建了一个简单的脚本(我是个新手(。除了一个小问题外,一切都在按需进行。

我的项目:我通过http:{服务器的ip}/?速度=999&时间=8&美分=50

这将在您的外壳中显示一个倒计时计时器。在这种情况下,从8倒计时到0。这很有效。当我发送{服务器的ip}/?速度=499&时间=8&美分=50,它倒计时的速度会快一倍。这是必要的。

我面临的问题:在倒计时期间,我需要能够向服务器发送新请求。它应该用新的值更新倒计时。目前它将创建2个倒计时。如果你读了剧本,这是正确的,但不是我需要的。我需要一些帮助如何更新现有的倒计时。

我的代码:

from flask import Flask, request
app = Flask(__name__)
import time
@app.route('/', methods = ["GET"])
def post():
speed = float(request.args["speed"])
thetime = request.args["time"]
cents = request.args["cents"]
print('n')
print('AccuView Digital:')
print('Speed:', speed,' Time:', thetime,' Cents:', cents)
print('-------------------------------')
def countdown(thetime):
while thetime:
mins, secs = divmod(thetime, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print('Tijd:', timer, end="r")
time.sleep((speed+1)/1000)
thetime -= 1
if thetime == 0:
print('Werp geld inn') 
countdown(int(thetime))
return '1'
app.run(host='192.168.1.107', port= 8090)

感谢

正如您自己所说,脚本运行良好。现在的情况是flask正在创建线程来满足您的请求。当您发送第一个请求时,flask在一个线程上启动一个计数器,当您发送另一个请求后,flask启动另一个线程,另一个计数器在该线程上运行。两个线程都有自己的值speedthetimecents,并且一个线程不能更改另一个线程中变量的值。

因此,我们要做的是从任何线程访问/修改变量的值。一个简单的解决方案是使用全局变量,以便所有线程都可以访问这些变量。

解决方案:

  1. speedthetimecents设为全局变量,以便它们可以从任何线程进行修改
  2. 当我们收到请求时,检查计数器是否已经在运行(我们可以通过检查全局变量thetime的值是否为0来完成(
  3. 我们知道现有计数器是否正在运行,现在只需更新全局变量的值即可
  4. 如果没有正在运行的现有计数器,则启动计数器(调用方法countdown()(。否则,我们不需要做任何事情(我们已经在上一步中更新了全局变量的值,因此更新了现有的计数器(

代码:

import time
from flask import Flask, request
app = Flask(__name__)
# We create global variables to keep track of the counter
speed = thetime = cents = 0

@app.route('/', methods=["GET"])
def post():
global speed, thetime, cents
# Check if previous counter is running
counter_running = True if thetime else False
thetime = int(request.args["time"])
speed = float(request.args["speed"])
cents = request.args["cents"]
print('n')
print('AccuView Digital:')
print('Speed:', speed, ' Time:', thetime, ' Cents:', cents)
print('-------------------------------')
def countdown():
global thetime, speed
while thetime:
mins, secs = divmod(thetime, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print('Tijd:', timer, end="r")
time.sleep((speed + 1) / 1000)
thetime -= 1
if thetime == 0:
print('Werp geld inn')
# If existing counter is running, then we don't start another counter
if not counter_running:
countdown()
return '1'

app.run(host='192.168.1.107', port= 8090)

代码(这样我们就可以中断睡眠(:

import time
import threading
from flask import Flask, request
app = Flask(__name__)
# We create global variables to keep track of the counter
speed = thetime = cents = 0
sleep_speed = threading.Event()

@app.route('/', methods=["GET"])
def post():
global speed, thetime, cents, sleep_speed
# Check if previous counter is running
counter_running = True if thetime else False
thetime = int(request.args["time"])
speed = float(request.args["speed"])
cents = request.args["cents"]
# Make sure to interrupt counter's sleep
if not sleep_speed.is_set():
sleep_speed.set()
sleep_speed.clear()
print('n')
print('AccuView Digital:')
print('Speed:', speed, ' Time:', thetime, ' Cents:', cents)
print('-------------------------------')
def countdown():
global thetime, speed
while thetime:
mins, secs = divmod(thetime, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print('Tijd:', timer, end="r")
sleep_speed.wait((speed + 1) / 1000)
thetime -= 1
if thetime == 0:
print('Werp geld inn')
# If existing counter is running, then we don't start another counter
if not counter_running:
countdown()
return '1'

app.run(host='0.0.0.0', port=8090)

最新更新