fix broken pipe

This commit is contained in:
olli 2024-03-27 22:49:30 +01:00
parent 5280224a9c
commit d1c32d32c6

View File

@ -1,6 +1,5 @@
function g_python { function g_python {
unset g_python_result
g_rnd=$$ g_rnd=$$
local g_python_jobs local g_python_jobs
@ -8,56 +7,58 @@ function g_python {
# Use python in backround for multiple python commands running much faster # Use python in backround for multiple python commands running much faster
#if [ -z "${g_python_running}" ] #if [ -z "${g_python_running}" ]
if ! [[ ${g_python_jobs[*]} =~ python-pipeexec.py ]] if [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]]
then then
mkdir -p ${g_tmp}/${g_rnd} mkdir -p ${g_tmp}/${g_rnd}
if [ -s ${g_tmp}/${g_rnd}/python-error ] if [ -s ${g_tmp}/${g_rnd}/python-error ]
then then
g_echo_error "From last python run: $(cat ${g_tmp}/${g_rnd}/python-error)" g_echo_error "From last python run: $(cat ${g_tmp}/${g_rnd}/python-error)"
rm -f ${g_tmp}/${g_rnd}/python-error
fi fi
[ -p ${g_tmp}/${g_rnd}/python-in ] || mkfifo ${g_tmp}/${g_rnd}/python-in [ -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 #[ -p ${g_tmp}/${g_rnd}/python-out ] || mkfifo ${g_tmp}/${g_rnd}/python-out
echo "while 1: echo "while 1:
exec(open(\"${g_tmp}/${g_rnd}/python-in\").read()) exec(open(\"${g_tmp}/${g_rnd}/python-in\").read())
print('DONE')
" >${g_tmp}/${g_rnd}/python-pipeexec.py " >${g_tmp}/${g_rnd}/python-pipeexec.py
# python stream channel # 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 & } { 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" g_python_running="true"
fi fi
# do python # do python
echo "$@" >${g_tmp}/${g_rnd}/python-in & >${g_tmp}/${g_rnd}/python-out
sleep 0.02 >${g_tmp}/${g_rnd}/python-error
echo $@ >${g_tmp}/${g_rnd}/python-in
mapfile -t g_python_jobs < <(jobs -r) while true
if ! [[ ${g_python_jobs[*]} =~ python-pipeexec.py ]]
then
g_echo_error "$(cat ${g_tmp}/${g_rnd}/python-error)"
rm ${g_tmp}/${g_rnd}/python-error
unset g_python_running
return 1
fi
if [ -n "$g_python_waitforoutput" ]
then
until [ -n "$g_python_result" ]
do do
read -t 0.2 g_python_result <${g_tmp}/${g_rnd}/python-out
[ -s "${g_tmp}/${g_rnd}/python-error" ] && break # Check for output
done if [ -s ${g_tmp}/${g_rnd}/python-out ]
unset g_python_waitforoutput then
else unset g_python_result
read -t0.2 g_python_result <${g_tmp}/${g_rnd}/python-out 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 fi
if [ -s "${g_tmp}/${g_rnd}/python-error" ] # 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 then
g_echo_error "$(cat ${g_tmp}/${g_rnd}/python-error)" g_echo_error "Python Progress not running:
rm ${g_tmp}/${g_rnd}/python-error $(cat ${g_tmp}/${g_rnd}/python-error)"
return 1 return 1
fi fi
# sleep a short time
sleep 0.1
done
} }