gaboshlib/gaboshlib/g_python.sh
2024-03-27 22:49:30 +01:00

65 lines
1.7 KiB
Bash

function g_python {
g_rnd=$$
local g_python_jobs
mapfile -t g_python_jobs < <(jobs -r)
# Use python in backround for multiple python commands running much faster
#if [ -z "${g_python_running}" ]
if [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]]
then
mkdir -p ${g_tmp}/${g_rnd}
if [ -s ${g_tmp}/${g_rnd}/python-error ]
then
g_echo_error "From last python run: $(cat ${g_tmp}/${g_rnd}/python-error)"
fi
[ -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())
print('DONE')
" >${g_tmp}/${g_rnd}/python-pipeexec.py
# python stream channel
{ python3 -u ${g_tmp}/${g_rnd}/python-pipeexec.py >>${g_tmp}/${g_rnd}/python-out 2>>${g_tmp}/${g_rnd}/python-error & }
g_python_running="true"
fi
# do python
>${g_tmp}/${g_rnd}/python-out
>${g_tmp}/${g_rnd}/python-error
echo $@ >${g_tmp}/${g_rnd}/python-in
while true
do
# Check for output
if [ -s ${g_tmp}/${g_rnd}/python-out ]
then
unset g_python_result
mapfile -t g_python_result <${g_tmp}/${g_rnd}/python-out
if [[ ${g_python_result[-1]} == DONE ]]
then
# remove the DONE output (last array element
unset g_python_result[-1]
break
fi
fi
# Check for error
mapfile -t g_python_jobs < <(jobs -r)
if [ -s "${g_tmp}/${g_rnd}/python-error" ] || [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]]
then
g_echo_error "Python Progress not running:
$(cat ${g_tmp}/${g_rnd}/python-error)"
return 1
fi
# sleep a short time
sleep 0.1
done
}