Data flow and threads¶
State: where it lives¶
| State | Lives in | How |
|---|---|---|
| Saved values | GSettings | a key in the gschema, a property on Settings, and a row in Preferences if the user can change it |
| Fetched data and loading status | weather_store only |
its fields change only in _start_loading and _finish, on the main loop |
| Temporary UI state (selected tab, dialog contents) | the widget |
React with signals, don't poll: settings.connect("changed::<key>", …), weather_store.connect("notify::status", …) (or notify::is-ready).
The loading cycle¶
WeatherStore (state/weather_store.py) is a GObject.Object. Its status goes through these values:
| Status | Meaning |
|---|---|
idle |
nothing fetched yet |
loading |
a fetch is running; the data is cleared and is-ready is false |
ready |
the data of location is there; one data-updated signal, is-ready true |
offline |
no connection; the data of the last fetch is kept |
error |
the last fetch failed; error says why |
sequenceDiagram
participant UI as Window (main loop)
participant S as WeatherStore
participant W as fetch worker thread
participant API as services/ (Open-Meteo)
UI->>S: refresh()
S->>W: start thread (coords and unit read on the main loop)
W->>API: check_internet_connection(force=True)
W-->>S: idle_add(_start_loading)
W->>API: fetch_current
par one thread each
W->>API: fetch_hourly
W->>API: fetch_daily
W->>API: fetch_air_quality (optional)
end
W-->>S: idle_add(_finish, status, coords, data)
S-->>UI: notify::status, data-updated, notify::is-ready
Details worth knowing:
- Current weather first. If it fails, the whole fetch fails. The hourly and daily forecasts then run in parallel; air quality is optional: when it fails, the weather is still shown and the air-quality card says it isn't available.
- Each task gets
CONNECT_TIMEOUT + TIMEOUTseconds. The first required task still running after that, or the first error, is raised on the worker. - The store builds the models.
build_currentadds humidity, wind and pressure levels.add_hourly_fieldsfills the values the current API doesn't give (UV index, dew point, visibility) from this hour of the hourly forecast.build_air_pollutionadds this hour's US and EU AQI. - One fetch at a time.
refresh()joins a running fetch for the same city. For another city, it fetches that one when the running fetch ends and drops the old result. - The internet check is part of
refresh(), on its worker, so callers never block on it. The auto-refresh passescheck_online=False. - A hard refresh (
refresh(hard=True),Ctrl+Shift+R) first forgets the selected city's cached responses, on the worker too: the cache holds its lock while a request runs. - One emit per real change. A value built in several steps is emitted once, at the end.
- A new field is built in the worker (
_download) and set in_finish. - Saved locations. The Locations list uses
weather_store.refresh_saved_locations(coords, on_done): onefetch_manyrequest on its own thread, results handed back withon_doneon the main loop.
The main window reacts in _on_store_status (ui/window.py): loading shows the loader, ready rebuilds the content grid, offline shows error_no_internet, error shows error_api with weather_store.error. There are no fetch callbacks.
Threads¶
- Never touch GTK from a worker thread. Hand results back with
GLib.idle_addand emit signals from there, so subscribers never see a thread. - Threads stay inside the store. UI code never sees one, and never starts its own for weather data.
- Don't wait on the main thread. No
threading.Event,joinor blocking network call there. A lock for shared data is held only while assigning or reading, never while calling the network, emitting or callingGLib.
Caching¶
Every API function is wrapped in @cached (core/cache.py): a TTL plus LRU cache, DATA_CACHE_TTL = 60 s and at most DATA_MAX_ENTRIES = 128 entries. Only non-None results are cached, so a failed request is retried at once. The cache key is the function's arguments; unit is one of them, so switching units never returns data in the old ones. cache_clear(lat, lon) forgets one place in every unit.
Auto refresh¶
AutoRefreshTimer (ui/gtk_utils.py), started by app.py, calls weather_store.refresh(check_online=False) every AUTO_REFRESH_MINUTES (10), always. There is no setting for it. The overview only sees a changed forecast after a refresh.
No leaks, no busy loops¶
The content grid is rebuilt on every refresh, so widgets must never be kept alive by their own handlers.
- Connect handlers on rebuilt widgets with
weak_connect. A closure keeps everything it captures: capture plain values, notselfor widgets. - Set draw functions with
safe_set_draw_func. The object passed to it is kept alive by its drawing area, so it must not also hold the area's parent. Hand out the drawing area itself. - Remove
GLib.timeout_addsources when their widget goes. Never pass widgets as extra arguments toidle_add/timeout_addfor a source that can outlive them. - Disconnect
settingshandlers you connect from short-lived widgets. Dialogs are closed, not destroyed, so connect withweak_connector let go of their widgets onclosed. - Never poll the main loop. No
idle_add/timeout_addcallback that returnsSOURCE_CONTINUEwhile it waits for UI state. Widgets are often built hidden behind the loader and may be thrown away unseen, so such a callback can spin at 100% CPU forever. Wait for a signal instead (Gtk.Adjustmentchanged,map/realize,notify::<property>) and disconnect one-shot handlers once they have run. - The only repeating timers are real clocks and intervals (the compact clock, the day/night card,
AutoRefreshTimer). - Keep draw functions light: compute in
__init__, only draw indraw.
After touching refresh, rendering, signals or main-loop sources, run Ctrl+R several times quickly (and switch location) and watch top -p <pid>: CPU must settle near 0% once loading ends and RSS must not keep climbing. In the Flatpak the process is python3 /app/bin/mousam, not the bwrap wrappers.