run interactive python shell from bash

This commit is contained in:
olli 2024-03-24 11:34:57 +01:00
parent 796cd3e114
commit e0b15e0804

37
gaboshlib/g_python.sh Normal file
View File

@ -0,0 +1,37 @@
function g_python {
unset g_python_result
g_tmp=/tmp
g_rnd=$$
# Use python in backround for multiple python commands running much faster
if [ -z "${g_python_running}" ]
then
mkdir -p ${g_tmp}/${g_rnd}
[ -p ${g_tmp}/${g_rnd}/python-in ] || mkfifo ${g_tmp}/${g_rnd}/python-in
[ -p ${g_tmp}/${g_rnd}/python-out ] || mkfifo ${g_tmp}/${g_rnd}/python-out
echo "while 1:
exec(open(\"${g_tmp}/${g_rnd}/python-in\").read())
" >${g_tmp}/${g_rnd}/python-pipeexec.py
# bc stream channel
{ python3 -u ${g_tmp}/${g_rnd}/python-pipeexec.py >${g_tmp}/${g_rnd}/python-out 2>&1 & }
g_python_running="true"
fi
# do python
local g_python_jobs
mapfile -t g_python_jobs < <(jobs -r)
#echo "Jobs $g_python_jobs"
#if jobs -r | grep -q python-pipeexec.py
if [[ $g_python_jobs =~ python-pipeexec.py ]]
then
echo $@ >${g_tmp}/${g_rnd}/python-in &
read -t 0.1 g_python_result <${g_tmp}/${g_rnd}/python-out
echo "RESULT: $g_python_result"
else
echo "FAIL: $g_python_result"
unset g_python_running
fi
}