How do I get a persistent python session from the command line?
I would like to get this behavior from the shell:
$ python --new-session -c ''
$ python --use-session -c 'x = 42'
$ python --use-session -c 'print x'
42
Why?
I use a lot of command line helpers like mako-render
and jinja
to generate files for my (C / C ++) project. Every time Python is called, all modules used must be imported into the workspace, which takes time. By using persistent workspace across my Python calls I can save a lot of processing time.
My current solution is to use shelve
to store everything I can between my sessions, but this is not convenient and I am looking for a less complicated solution.
I know something is possible using Jupyter core. Unfortunately, starting a new Python session connected to an existing kernel takes about 5-10 seconds on my system.
source to share
The following bash script is a rough approximation of what you want:
python session:
#!/usr/bin/env bash
if [[ $# -ne 1 ]]
then
echo 1>&2 "Usage: python-session <session-name>"
exit 1
fi
sessionname="$1"
sessiondir=/tmp/python-session."$sessionname"
stdin="$sessiondir/stdin"
stdout="$sessiondir/stdout"
endsession()
{
echo Exiting python session "$sessionname"
rm -rf "$sessiondir"
}
newsession()
(
echo "Starting new session $sessionname"
mkdir "$sessiondir"
trap "endsession" EXIT
touch "$stdin"
touch "$stdout"
tail -f "$stdin"|python -i -c "import sys; sys.ps1=sys.ps2=''">"$stdout"
)
if [ ! -d "$sessiondir" ]
then
newsession & disown
while [ ! -e "$stdout" ]; do sleep 0.01; done
fi
echo "Connected to python session $1 (leave the session by typing CTRL-D)"
tail -f -n 0 "$stdout"&
tailpid=$!
trap "kill $tailpid" EXIT
cat >> "$stdin"
Demonstration:
$ ./python-session 1
Starting new session 1
Connected to python session 1 (leave the session by typing CTRL-D)
x=123
$ ./python-session 1
Connected to python session 1 (leave the session by typing CTRL-D)
print x
123
$ ./python-session 2
Starting new session 2
Connected to python session 2 (leave the session by typing CTRL-D)
print x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
x='abc'
$ ./python-session 1
Connected to python session 1 (leave the session by typing CTRL-D)
print x
123
$ ./python-session 2
Connected to python session 2 (leave the session by typing CTRL-D)
print x
abc
exit()
Exiting python session 2
$ ./python-session 1
Connected to python session 1 (leave the session by typing CTRL-D)
exit()
Exiting python session 1
$
Implementation project limitations
-
you must stop the session by entering a command
exit()
followed by an additional (even blank) line. After that, you still need to click CTRL-Dto disconnect from the already non-existent session -
python session standard error is not written
source to share