This commit is contained in:
olli 2024-06-26 19:00:38 +02:00
parent 0f7cddd5a6
commit bf5c616f01

View File

@ -12,83 +12,103 @@ function g_python {
then then
local python_input="$g_tmp/$$-g_python_input" local python_input="$g_tmp/$$-g_python_input"
local python_output="$g_tmp/$$-g_python_output" local python_output="$g_tmp/$$-g_python_output"
local python_error="$g_tmp/$$-g_python_error"
# create fifo pipe # create fifo pipe
[ -p "$python_input" ] || mkfifo "$python_input" [ -p "$python_input" ] || mkfifo "$python_input"
[ -p "$python_output" ] || mkfifo "$python_output" [ -p "$python_output" ] || mkfifo "$python_output"
[ -p "$python_error" ] || mkfifo "$python_error"
# run bc in background und switch i/o to pipes # run bc in background und switch i/o to pipes
python3 -iu < "$python_input" > "$python_output" 2>/dev/null & python3 -iuq < "$python_input" > "$python_output" 2> "$python_error" &
# store in filedescriptiors # store in filedescriptiors
exec {g_fd_python_in}> "$python_input" exec {g_fd_python_in}> "$python_input"
exec {g_fd_python_out}< "$python_output" exec {g_fd_python_out}< "$python_output"
exec {g_fd_python_error}< "$python_error"
fi fi
# send calculation and read result
# send python command
echo "$@ echo "$@
print('')" >&${g_fd_python_in} print('')" >&${g_fd_python_in}
# read output
read -u ${g_fd_python_out} g_python_result read -u ${g_fd_python_out} g_python_result
local g_errline
local g_err_array
# look for error
while read -t 0.001 -u ${g_fd_python_error} g_errline
do
[[ "$g_errline" =~ \>\>\>$ ]] && break
[ -z "$g_errline" ] && break
echo $g_errline 1>&2
g_err_array+=("$g_errline")
done
[ -n "$g_err_array" ] && return 1
} }
#function g_python {
# function g_python_old {
# local g_python_tmp=${g_tmp}/$$
# g_python_out=${g_python_tmp}/python-out local g_python_tmp=${g_tmp}/$$
# local g_python_in=${g_python_tmp}/python-in g_python_out=${g_python_tmp}/python-out
# local g_python_error=${g_python_tmp}/python-error local g_python_in=${g_python_tmp}/python-in
# local g_python_jobs local g_python_error=${g_python_tmp}/python-error
# mapfile -t g_python_jobs < <(jobs -r) local g_python_jobs
# unset g_python_result 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}" ] # Use python in backround for multiple python commands running much faster
# if [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]] #if [ -z "${g_python_running}" ]
# then if [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]]
# mkdir -p ${g_python_tmp} then
# if [ -s ${g_python_error} ] mkdir -p ${g_python_tmp}
# then if [ -s ${g_python_error} ]
# g_echo_error "From last python run: $(cat ${g_python_error})" then
# fi g_echo_error "From last python run: $(cat ${g_python_error})"
# [ -p ${g_python_in} ] || mkfifo ${g_python_in} fi
# echo "while 1: [ -p ${g_python_in} ] || mkfifo ${g_python_in}
# exec(open(\"${g_python_in}\").read()) echo "while 1:
# print('DONE') exec(open(\"${g_python_in}\").read())
#" >${g_python_tmp}/python-pipeexec.py 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} & } # python stream channel
# g_python_running="true" { python3 -u ${g_python_tmp}/python-pipeexec.py >>${g_python_out} 2>>${g_python_error} & }
# fi g_python_running="true"
# fi
# # do python
# >${g_python_out} # do python
# >${g_python_error} >${g_python_out}
# echo $@ >${g_python_in} >${g_python_error}
# echo $@ >${g_python_in}
# while true
# do while true
# do
# # Check for output
# if [ -s ${g_python_out} ] # Check for output
# then if [ -s ${g_python_out} ]
# mapfile -t g_python_result <${g_python_out} then
# if [[ ${g_python_result[-1]} == DONE ]] mapfile -t g_python_result <${g_python_out}
# then if [[ ${g_python_result[-1]} == DONE ]]
# # remove the DONE output (last array element then
# unset g_python_result[-1] # remove the DONE output (last array element
# break unset g_python_result[-1]
# fi break
# fi fi
# fi
# # Check for error
# mapfile -t g_python_jobs < <(jobs -r) # Check for error
# if [ -s "${g_python_error}" ] || [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]] mapfile -t g_python_jobs < <(jobs -r)
# then if [ -s "${g_python_error}" ] || [[ ${g_python_jobs[*]} != *python-pipeexec.py* ]]
# g_echo_error "Python Progress not running: then
#$(cat ${g_python_error})" g_echo_error "Python Progress not running:
# return 1 $(cat ${g_python_error})"
# fi return 1
# fi
# # sleep a short time
# sleep 0.1 # sleep a short time
# done sleep 0.1
# done
#}
}