Python:全局变量未通过函数获取 affected/changed
Python: Global variable isn't getting affected/changed by function
我正在处理的 Flask 应用程序出现问题。
名为 buttonNum 的全局变量应该随着每次调用 button() 而改变;但是,ardCom() 中的 buttonNum 不受影响,不承认使用 button() 对其所做的更改。我在网上找到了有关如何使用全局变量的示例,据我所知,这应该可行。
感谢所有帮助。
谢谢
import time
import json
import serial
import flask
from multiprocessing import Process
from flask import Flask, request, render_template, make_response
buttonNum = 0
app = Flask(__name__)
@app.route('/')
def default():
headers = {'Content-Type': 'text/html'}
return make_response(render_template('index.html'),200,headers)
@app.route('/setup', methods=['POST'])
def setup():
data = request.get_json
return {'result' : "Success"}
@app.route('/button', methods=['POST'])
def button():
global buttonNum
buttonNum= int(request.form['number'])
typeOf = int(request.form['type'])
value = int(request.form['value'])
return "success"
def ardCom():
global buttonNum
county = 1
ph = b''
ph = ser.read()
if ph == b'S':
ser.write(ph)
while (ph != b'E'):
ph = ser.read()
if ph == b'E':
ser.write(ph)
break
#print(str(buttonNum))
if ph == b'\x00' and buttonNum != county:
writeOut(b'\x00')
county += 1
if ph == b'\x01' or buttonNum == county:
writeOut(b'\x01')
print("Button", end = " ")
print(county, end = " ")
print("has been pressed.")
county += 1
def writeOut(datByte):
ser.write(datByte)
def runApi():
app.run(host='0.0.0.0')
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()
time.sleep(1)
apiProcess = Process(target=runApi)
apiProcess.start()
while True:
ardCom()
当 flask 服务器运行时,它可能会运行一些实例。为了使您的代码有效,您可以将值存储在文件中(如 json)或数据库中。
我正在处理的 Flask 应用程序出现问题。
名为 buttonNum 的全局变量应该随着每次调用 button() 而改变;但是,ardCom() 中的 buttonNum 不受影响,不承认使用 button() 对其所做的更改。我在网上找到了有关如何使用全局变量的示例,据我所知,这应该可行。
感谢所有帮助。 谢谢
import time
import json
import serial
import flask
from multiprocessing import Process
from flask import Flask, request, render_template, make_response
buttonNum = 0
app = Flask(__name__)
@app.route('/')
def default():
headers = {'Content-Type': 'text/html'}
return make_response(render_template('index.html'),200,headers)
@app.route('/setup', methods=['POST'])
def setup():
data = request.get_json
return {'result' : "Success"}
@app.route('/button', methods=['POST'])
def button():
global buttonNum
buttonNum= int(request.form['number'])
typeOf = int(request.form['type'])
value = int(request.form['value'])
return "success"
def ardCom():
global buttonNum
county = 1
ph = b''
ph = ser.read()
if ph == b'S':
ser.write(ph)
while (ph != b'E'):
ph = ser.read()
if ph == b'E':
ser.write(ph)
break
#print(str(buttonNum))
if ph == b'\x00' and buttonNum != county:
writeOut(b'\x00')
county += 1
if ph == b'\x01' or buttonNum == county:
writeOut(b'\x01')
print("Button", end = " ")
print(county, end = " ")
print("has been pressed.")
county += 1
def writeOut(datByte):
ser.write(datByte)
def runApi():
app.run(host='0.0.0.0')
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()
time.sleep(1)
apiProcess = Process(target=runApi)
apiProcess.start()
while True:
ardCom()
当 flask 服务器运行时,它可能会运行一些实例。为了使您的代码有效,您可以将值存储在文件中(如 json)或数据库中。