Let's be honest, trying to manage external processes or subprocesses from a Streamlit app is tricky. The execution model makes it difficult to keep track of which processes are supposed to be running and which ones aren't. Half the time you end up spawning twenty different processes all trying to access the same file and stomping all over one another.
Streamlit Process Manager attempts to make a that a bit easier. It looks something like this:
And here's the simplest program (you have to write my_program.py
yourself):
import streamlit_process_manager as spm
my_program = ["python", "-u", "my_program.py"]
output_file = "program_output.txt"
process = spm.run(my_program, output_file=output_file)
# in my_program.py
# -------------------------------------------
import time
try:
for x in range(100):
print(f"Running... {x}%")
time.sleep(0.1)
finally:
print("I'm done!")
⚠️ DISCLAIMER⚠️ Runing processes on your machine from a public-facing application like a Streamlit app can be incredibly risky and comes with all sorts of security considerations. Use this project on public apps at your own risk, and never inject user-provided values or arguments into your processes without serious consideration of all the possible injection attacks you might be opening your server up to.
Alpha: pip install streamlit-process-manager==0.0.1a
Latest: pip install git+https://github.com/Asaurus1/streamlit-process-manager
Requires
Python 3.8.*
or aboveStreamlit 1.30.*
or abovepsutil 5.9.8
or above
This project uses pdm
for most of its development tooling. Recommended environment for development is kept in the pdm.lock
file.
Ensure you have python3.12
installed, then run
pip install pdm
pdm venv create 3.12
pdm install --dev --group test --group lint
to get started, then run
pdm check
This will run the linter, type checker, and tester. If everything passes then you are set up and ready to go.
Once you have your environment installed, you can run pdm example
to start streamlit and open the example page.
This is a good introduction to how streamlit-process-manager
works, and has several code examples you can read
through to understand the recommnded practices for using the library.
If you have multiple versions of Python available on your system you can use tox
to run tests
on all of them at once. tox
should be installed when you install the --dev
dependencies so
you can just run it. Any Python's not available on your system will be skipped.
pdm run tox
Copyright 2024 by Alexander Martin
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.