From 1e5e310fc876b666d5f16dd97afe9e286347f19c Mon Sep 17 00:00:00 2001 From: olli <> Date: Wed, 26 Jun 2024 11:10:27 +0200 Subject: [PATCH] fixes --- gaboshlib.include | 2 +- gaboshlib/g_calc.sh | 5 +- gaboshlib/g_python.sh | 145 +++++++++++++++++++++++++----------------- 3 files changed, 92 insertions(+), 60 deletions(-) diff --git a/gaboshlib.include b/gaboshlib.include index 17973fa..6adad40 100644 --- a/gaboshlib.include +++ b/gaboshlib.include @@ -34,7 +34,7 @@ g_tmp="$g_tmp/g_$g_scriptname-$$" # START and EXIT Notification g_trap_exit="g_logger EXITING $g_scriptname ; rm -r $g_tmp ; g_kill_all_background_jobs >/dev/null 2>&1" -trap "$g_trap_exit" EXIT +trap "$g_trap_exit" INT TERM EXIT g_syslogtag="g_bash-script:$g_scriptname[$$]" [ $g_scriptname = "bash" ] || g_logger STARTING $g_scriptname diff --git a/gaboshlib/g_calc.sh b/gaboshlib/g_calc.sh index 8c3612a..c2269f9 100644 --- a/gaboshlib/g_calc.sh +++ b/gaboshlib/g_calc.sh @@ -5,8 +5,11 @@ function g_calc { unset g_calc_result + local g_jobs + mapfile -t g_jobs < <(jobs -r) + # check if bc is already running - if [[ -z "$g_fd_bc_in" || -z "$g_fd_bc_out" ]] + if [[ -z "$g_fd_bc_in" || -z "$g_fd_bc_out" || ${g_jobs[*]} != *bc* ]] then local bc_input="$g_tmp/$$-g_bc_input" local bc_output="$g_tmp/$$-g_bc_output" diff --git a/gaboshlib/g_python.sh b/gaboshlib/g_python.sh index 0085313..be013ba 100644 --- a/gaboshlib/g_python.sh +++ b/gaboshlib/g_python.sh @@ -1,65 +1,94 @@ -function g_python { +function g_python_test { + + # function for running python in the background + # $g_fd_python_in $g_fd_python_out have to be global - local g_python_tmp=${g_tmp}/$$ - g_python_out=${g_python_tmp}/python-out - local g_python_in=${g_python_tmp}/python-in - local g_python_error=${g_python_tmp}/python-error - local g_python_jobs - mapfile -t g_python_jobs < <(jobs -r) unset g_python_result - # Use python in backround for multiple python commands running much faster - #if [ -z "${g_python_running}" ] - if [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]] + local g_jobs + mapfile -t g_jobs < <(jobs -r) + # check if python is already running + if [[ -z $g_fd_python_in || -z $g_fd_python_out || ${g_jobs[*]} != *python3* ]] then - mkdir -p ${g_python_tmp} - if [ -s ${g_python_error} ] - then - g_echo_error "From last python run: $(cat ${g_python_error})" - fi - [ -p ${g_python_in} ] || mkfifo ${g_python_in} - echo "while 1: - exec(open(\"${g_python_in}\").read()) - print('DONE') -" >${g_python_tmp}/python-pipeexec.py - - # python stream channel - { python3 -u ${g_python_tmp}/python-pipeexec.py >>${g_python_out} 2>>${g_python_error} & } - g_python_running="true" + local python_input="$g_tmp/$$-g_python_input" + local python_output="$g_tmp/$$-g_python_output" + # create fifo pipe + [ -p "$python_input" ] || mkfifo "$python_input" + [ -p "$python_output" ] || mkfifo "$python_output" + # run bc in background und switch i/o to pipes + python3 -iu < "$python_input" > "$python_output" 2>/dev/null & + # store in filedescriptiors + exec {g_fd_python_in}> "$python_input" + exec {g_fd_python_out}< "$python_output" fi - - # do python - >${g_python_out} - >${g_python_error} - echo $@ >${g_python_in} - - while true - do - - # Check for output - if [ -s ${g_python_out} ] - then - mapfile -t g_python_result <${g_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_python_error}" ] || [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]] - then - g_echo_error "Python Progress not running: -$(cat ${g_python_error})" - return 1 - fi - - # sleep a short time - sleep 0.1 - done - + # send calculation and read result + echo "$@ +print('')" >&${g_fd_python_in} + read -u ${g_fd_python_out} g_python_result } +#function g_python { +# +# local g_python_tmp=${g_tmp}/$$ +# g_python_out=${g_python_tmp}/python-out +# local g_python_in=${g_python_tmp}/python-in +# local g_python_error=${g_python_tmp}/python-error +# local g_python_jobs +# mapfile -t g_python_jobs < <(jobs -r) +# unset g_python_result +# +# # 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_python_tmp} +# if [ -s ${g_python_error} ] +# then +# g_echo_error "From last python run: $(cat ${g_python_error})" +# fi +# [ -p ${g_python_in} ] || mkfifo ${g_python_in} +# echo "while 1: +# exec(open(\"${g_python_in}\").read()) +# print('DONE') +#" >${g_python_tmp}/python-pipeexec.py +# +# # python stream channel +# { python3 -u ${g_python_tmp}/python-pipeexec.py >>${g_python_out} 2>>${g_python_error} & } +# g_python_running="true" +# fi +# +# # do python +# >${g_python_out} +# >${g_python_error} +# echo $@ >${g_python_in} +# +# while true +# do +# +# # Check for output +# if [ -s ${g_python_out} ] +# then +# mapfile -t g_python_result <${g_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_python_error}" ] || [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]] +# then +# g_echo_error "Python Progress not running: +#$(cat ${g_python_error})" +# return 1 +# fi +# +# # sleep a short time +# sleep 0.1 +# done +# +#} +