i have a problem with my flask app. What is hapening is when i import my Client_OPCUA.py to my flask app (app.py) it runs infinitely and my flask app don't start. I import a list from Client_OPC.py and I use those list in app.py. Whant I whant is to run a Client OPCUA.py in my background.
Client_OPCUA.py
from opcua import Client
from opcua import ua
import time
url = 'opc.tcp://192.168.1.4:4840'
client = Client(url)
client.connect()
print('Client Connected')
lista = []
#global firstcicle
firstcicle = 1
print('lista antes da função == ' + str(lista))
print('Num ciclo antes da função =' + str(firstcicle))
def Client_OPCUA():
firstcicle = 1
while True:
print('ciclo =' + str(firstcicle))
BussyFQ2_IRB140 = client.get_node('ns=4;i=4')
StateBussyFQ2_IRB140 = BussyFQ2_IRB140.get_value()
print('Estado Bussy: ' + str(StateBussyFQ2_IRB140))
if firstcicle == 1:
lista.append(StateBussyFQ2_IRB140)
else:
lista[0] = StateBussyFQ2_IRB140
ErrorFQ2_IRB140 = client.get_node('ns=4;i=5')
StateErrorFQ2_IRB140 = ErrorFQ2_IRB140.get_value()
print('Estado Bussy: ' + str(StateErrorFQ2_IRB140))
if firstcicle == 1:
lista.append(StateErrorFQ2_IRB140)
else:
lista[1] = StateErrorFQ2_IRB140
print('lista dentro da função == ' + str(lista))
firstcicle = firstcicle +1
time.sleep(1)
if firstcicle == 1:
Client_OPCUA()
app.py
print('from flask import Flask, render_template')
from flask import Flask, render_template
print('import threading')
import threading
print('from Client_OPCUA import Client_OPCUA')
from Client_OPCUA import Client_OPCUA
print('from Client_OPCUA import lista')
from Client_OPCUA import lista
thread = threading.Thread(target = Client_OPCUA)
print('thread = threading.Thread(target = Client_OPCUA)')
thread.daemon = True
thread.start()
print('lista dentro do app.py == ' + str(lista))
#from Client_OPCUA import lista
app = Flask(__name__)
StateBussyFQ2_IRB140 = lista[0]
StateErrorFQ2_IRB140 = lista[1]
#lista1 = []
@app.route('/')
def index():
StateBussyFQ2_IRB140 = lista[0]
return render_template('index.html',
StateBussyFQ2_IRB140 = StateBussyFQ2_IRB140,
StateErrorFQ2_IRB140 = StateErrorFQ2_IRB140)
@app.route("/forward/", methods=['POST'])
def move_forward():
forward_message = "Moving Forward..."
return render_template('index.html', forward_message=forward_message);
@app.route("/OK_Operador/", methods=['POST'])
def OK_Operador():
OK_Operador = 1
#lista1[0] = OK_Operador
return render_template('index.html', OK_Operador=OK_Operador);
And I have seen this links in stackoverflow:
# Running a Flask server parallel to main app
# https://stackoverflow.com/questions/36617859/running-a-flask-server-parallel-to-main-app
# Flask - Calling python function on button OnClick event
# https://stackoverflow.com/questions/42601478/flask-calling-python-function-on-button-onclick-event
# Python Spawn a Thread with Threading and kill when main finishes
# https://stackoverflow.com/questions/13372508/python-spawn-a-thread-with-threading-and-kill-when-main-finishes
# Can I have Python code to continue executing after I call Flask app.run?
# https://stackoverflow.com/questions/47005647/can-i-have-python-code-to-continue-executing-after-i-call-flask-app-run