forked from SWISDATA/processintel
nix.module: one app process serves all users, so mining uses only one CPU core #65
Labels
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ffrauenberger/ProcessIntel#65
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Severity: improvement (high for the hosted service: 10 concurrent mining users make every run about 10× slower, while the other cores stay idle)
Where
module.nix:191-199— the service starts oneprocessintelprocessmodule.nix:267-275— the nginx upstream has one serverapp/__main__.py— oneuvicorn.run(app, …)servesstreamlit.Appfor all usersapp/ui/base_algorithm_ui/base_algorithm_view.py:188— mining runs in the session's script threadProblem
All users share one Python process. Streamlit runs each session's script in its own thread (
ScriptRunner.scriptThread), but all threads share the GIL. Most mining code is pure Python. So all users together get the CPU time of one core, however many cores the host has (services#one-process-is-not-a-machine).#31 and #54 state this, but it was never measured. Here is the measurement.
Setup:
GeneticMining(log)and callsgenerate_graph, as the controller does.tests/testcsv/CallcenterExample.csv(416 variants, 3,885 cases), with population 100 and 200 generations.ProcessPoolExecutor)The total time grows linearly with the number of sessions (about n × 1.3 s), so the runs are serialised on one core. The same ten runs in ten processes finish 5.4× faster. A synthetic check (1 vs. 10 threads) gave about 8× slower runs for a pure-Python loop, and about 4× for NumPy matrix products, which release the GIL.
What this means:
#54 is the code-level fix: mining in a process pool. It needs #46 first, so it takes time. The deployment can spread users over cores now, without code changes. Neither #31 nor #54 covers that.
Fix
instancesoption tomodule.nix(default 1). Run oneprocessintelprocess per instance, each on its own socket (e.g. a template unitprocessintel@<n>).ip_hash, or a cookie hash). A Streamlit session lives in the memory of one process, and its WebSocket and uploads must reach that same process.uvicorn.run(app, workers=N)is no alternative: Uvicorn needs an import string for workers, and it would not keep sessions sticky.MemoryMax=, #31), so one large upload restarts only one instance.scripts/(not a unit test, because timings vary). Record the capacity per instance in the deployment docs (testing#measure-before-optimizing).This does not replace #54: within one instance, sessions still share a core. It also does not replace the limits of #31 (
services#everything-is-bounded).Check
instances = 4, ten concurrent Genetic runs from different clients finish in about a quarter of the single-instance time.nix develop .#processintel --impure, withPYTHONPATH=.:$PYTHONPATH:Related: #31, #54, #39, #45.
from a session on 2026-10-08 (measured on commit
6bcc646); draft inissues/PERF-010-all-sessions-share-one-cpu-core.md