Python Flask 在新请求后更新 var

Python Flask update var after new request

我在 Python 中创建了一个简单的脚本(我是新手)。除一个小问题外,一切正常。

我的项目: 我通过 http 向我的服务器发送请求: {服务器IP}/?speed=999&time=8¢s=50

这将在您的 shell 中显示倒数计时器。在这种情况下从 8 倒数到 0。这很好用。当我发送 {ip-of-server}/?speed=499&time=8¢s=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 in\n') 
    countdown(int(thetime))
    return '1'

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

谢谢

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

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

解法:

  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 in\n')

    # 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 in\n')

    # 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)