首页 > 解决方案 > 如何在 Python 中与 Flask 并行运行 python 文件

问题描述

我的烧瓶应用程序有问题。正在发生的事情是,当我将 Client_OPCUA.py 导入到我的烧瓶应用程序 (app.py) 时,它会无限运行并且我的烧瓶应用程序无法启动。我从 Client_OPC.py 导入一个列表,并在 app.py 中使用这些列表。我想要的是在我的后台运行客户端 OPCUA.py。

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

应用程序.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);

我在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

标签: pythonmultithreadingflask

解决方案


推荐阅读