【问题标题】:How to check for changes in url parameter inside loop with Python Flask如何使用 Python Flask 检查循环内 url 参数的变化
【发布时间】:2017-07-18 15:59:09
【问题描述】:

我正在尝试使用 Flask 启动和停止作为 Python Web 应用程序运行的服务。该服务涉及一个连续执行的循环,侦听麦克风输入并在输入超过预定义阈值时采取一些措施。当使用 /on 参数传递 url 时,我可以让程序开始执行,但是一旦启动,我就找不到停止它的方法。我尝试使用 request.args.get 来监视 url 参数的状态并观察它从 /on 变为 /off,但由于某种原因,程序没有注册我已更改查询字符串以尝试停止执行。当 url 参数从 /on 更改为 /off 时,是否有更好的方法来执行我的代码并让它停止?非常感谢任何帮助!

import alsaaudio, time, audioop
import RPi.GPIO as G
import pygame
from flask import Flask
from flask import request
app = Flask(__name__)

G.setmode(G.BCM)
G.setup(17,G.OUT)
pygame.mixer.init()
pygame.mixer.music.load("/home/pi/OceanLoud.mp3")

@app.route('/autoSoothe', methods=['GET','POST'])
def autoSoothe():
        toggle = request.args.get('state')
        print(toggle)
        if toggle == 'on':
# Open the device in nonblocking capture mode. The last argument could
# just as well have been zero for blocking mode. Then we could have
# left out the sleep call in the bottom of the loop
                inp =   alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK,'null',0)

# Set attributes: Mono, 8000 Hz, 16 bit little endian samples
            inp.setchannels(1)
            inp.setrate(8000)
            inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)

# The period size controls the internal number of frames per period.
# The significance of this parameter is documented in the ALSA api.
# For our purposes, it is suficcient to know that reads from the device
# will return this many frames. Each frame being 2 bytes long.
# This means that the reads below will return either 320 bytes of data
# or 0 bytes of data. The latter is possible because we are in nonblocking
# mode.
            inp.setperiodsize(160)

            musicPlay = 0

            while toggle == 'on':
                    toggle = request.args.get('state')
                    print(toggle)
                    if toggle == 'off':
                            break
    # Read data from device

                    l,data = inp.read()
                    if l:
                            try:
    # Return the maximum of the absolute value of all samples in a fragment.
                                    if audioop.max(data, 2) > 20000:
                                            G.output(17,True)
                                            musicPlay = 1
                                    else:
                                            G.output(17,False)

                                    if musicPlay == 1:
                                            pygame.mixer.music.play()
                                            time.sleep(10)
                                            pygame.mixer.music.stop()
                                            musicPlay = 0

                            except audioop.error, e:
                                    if e.message != "not a whole number of frames":
                                            raise e

                            time.sleep(.001)

    return toggle

if __name__ == "__main__":
     app.run(host='0.0.0.0', port=5000, debug=True)

【问题讨论】:

    标签: python flask raspberry-pi


    【解决方案1】:

    当客户端向 Flask 服务器发出 HTTP 请求时,客户端会发送一个请求并等待服务器的响应。这意味着当您发送state 参数时,客户端无法追溯更改它。

    有几种不同的方法可以实现您想要的行为。

    我首先想到的是使用一些异步代码。您可以拥有在state 为“on”时启动线程/进程然后完成请求的代码。该线程将运行您的音频循环。然后客户端可以发送另一个请求,但 state 处于“关闭”状态,这可能会提醒其他进程正常停止。

    Here 是关于多进程的一些信息;但是,有很多关于如何使用 Flask 使用 Celery 等做类似事情的信息。

    【讨论】:

    • 非常感谢 EpicDavi。我一定会看看这个!
    猜你喜欢
    • 2022-06-11
    • 2022-12-20
    • 2012-07-28
    • 2019-02-05
    • 2016-02-13
    • 1970-01-01
    • 2019-06-16
    • 2022-12-01
    • 1970-01-01
    相关资源
    最近更新 更多