-
This is the code for the file "weather04.py" from the textual\docs\examples\guide\workers folder import httpx
from rich.text import Text
from textual import work
from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widgets import Input, Static
from textual.worker import Worker
class WeatherApp(App):
"""App to display the current weather."""
CSS_PATH = "weather.tcss"
def compose(self) -> ComposeResult:
yield Input(placeholder="Enter a City")
with VerticalScroll(id="weather-container"):
yield Static(id="weather")
async def on_input_changed(self, message: Input.Changed) -> None:
"""Called when the input changes"""
self.update_weather(message.value)
@work(exclusive=True)
async def update_weather(self, city: str) -> None:
"""Update the weather for the given city."""
weather_widget = self.query_one("#weather", Static)
if city:
# Query the network API
url = f"https://wttr.in/{city}"
async with httpx.AsyncClient() as client:
response = await client.get(url)
weather = Text.from_ansi(response.text)
weather_widget.update(weather)
else:
# No city, so just blank out the weather
weather_widget.update("")
def on_worker_state_changed(self, event: Worker.StateChanged) -> None:
"""Called when the worker state changes."""
self.log(event)
if __name__ == "__main__":
app = WeatherApp()
app.run() Issue:The program lags as if there were no async or worker being used. Question:Do I have something configured wrong? Or should I open an issue? orz |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I just gave it a quick spin and it seems to be working fine to me. Perhaps you could start with giving more information about how and where you're running it; perhaps show the output of |
Beta Was this translation helpful? Give feedback.
-
Might just be the API was slow at the time? It's a free service, and isn't always responsive. |
Beta Was this translation helpful? Give feedback.
Sure, I get what the docs say, I was asking how you're seeing and experiencing this lag. For example, here's me (on macOS, not Windows 10, as I don't have Windows 10 to hand) running the above code and it seems to be responding just fine:
Screen.Recording.2024-03-04.at.16.27.26.mov
this is in stark contrast to running
weather01.py
(you can see it really struggling when I request cities that the backend obviously hasn't had a chance to cache):Screen.Recording.2024-03-04.at.16.32.42.mov