Reputation: 17
I have a main file GUI Taipy' with subpages. In one of these subpages, I'm using a class named 'Jiga' and within it, I'm using a thread to scan four physical buttons on a hardware interface.
When I use the following code:
thread1 = threading.Thread(target=scan_teclas, args=(state.Jiga,))
The code executes normally. However, within the thread function, I can only access the variables within the 'state.Jiga' class.
If I use the following code:
thread1 = threading.Thread(target=scan_teclas, args=(state,))
The code does not execute, and I encounter the following error:
"RuntimeError: Working outside of the application context. This typically means that you attempted to use functionality that requires the current application context. To solve this, set up an application context with app.app_context(). See the documentation for more information."
I suspect that the thread is operating outside the Flask or Taipy context, which is causing the problem. However, when using 'state.Jiga,' I don't encounter this issue. Is that correct?" How make the app_context?
Upvotes: 0
Views: 229
Reputation: 46
Multi-threading with Taipy is a complicated question; we are working on documenting it more. I might need more of your code to solve your issue accurately; maybe Florian Jacta has an idea for it. In the meantime, I have a simple example you could use as a base for doing your multi-threading code with Taipy:
Step 1: Install the latest dev release of Taipy
pip install taipy==3.0.0.dev3
Step 2: Run this script that runs a Taipy application with a socket waiting to receive information:
import socket
from threading import Thread
from taipy.gui import Gui, State, invoke_callback, get_state_id
HOST = "127.0.0.1"
PORT = 65432
# Socket handler
def client_handler(gui: Gui, state_id_list: list):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
while True:
if data := conn.recv(1024):
print("Data received: ", data.decode())
if hasattr(gui, "_server") and state_id_list:
invoke_callback(
gui, state_id_list[0], update_case_count, (int(data.decode()),)
)
else:
print("Connection closed")
break
# Gui declaration
state_id_list = []
Gui.add_shared_variable("case_count")
def on_init(state: State):
state_id = get_state_id(state)
if (state_id := get_state_id(state)) is not None and state_id != "":
state_id_list.append(state_id)
def update_case_count(state: State, val):
state.case_count = val
# state.broadcast("case_count", val)
case_count = 0
md = """
# Covid Tracker
Number of cases: <|{case_count}|>
"""
gui = Gui(page=md)
t = Thread(
target=client_handler,
args=(
gui,
state_id_list,
),
)
t.start()
gui.run(run_browser=False)
Step 3: Run this script simultaneously in another terminal that sends information to the app. The sent information should appear in real time on the app
# echo-client.py
from random import randint
import time
import socket
HOST = "127.0.0.1"
PORT = 65432
random_number = randint(1, 100000)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
s.sendall(str(random_number).encode())
random_number += randint(0, 50)
print(random_number)
time.sleep(5)
Let me know if this helps. Feel free to contact us with more context about your issue, especially the code.
Upvotes: 1