我正在嘗試在 bash 執行一些檔案時運行一個簡單的影片 python 腳本。但是,一旦 bash 腳本完成,影片腳本就不會正確終止。
python腳本完美運行。如果我使用在 Bash 中本機運行的不同影片,則 bash 腳本可以作業。但是如果我讓 bash 運行 python 腳本,它會永遠運行嗎?
重擊腳本:
#!/usr/bin/env bash
# Don't leave animation running if files executed.
unset anim_pid
trap 'kill $anim_pid 2>/dev/null' EXT INT
# Get animation script
animation() {
python "/home/solebay/Project/loading_animation.py"
}
animation & anim_pid=$!
# Scripts to execute
python "/home/solebay/Project/script_a" & pid1=$!
python "/home/solebay/Project/script_b" & pid2=$!
# wait for tasks to finish
wait $pid1 $pid2
kill $anim_pid 2>/dev/null
printf '\n *** End of script ***\n'
蟒蛇loading_animation.py:
#!/usr/bin/env python
import itertools
import threading
import time
import sys
# Please wait message
msg = "\nPlease wait a moment..."
for idx, i in enumerate(msg):
if idx <= 19:
sys.stdout.write(i)
sys.stdout.flush()
time.sleep(0.02)
else:
sys.stdout.write(i)
sys.stdout.flush()
time.sleep(0.6)
# Spinning part
for c in itertools.cycle(['|', '/', '-', '\\']):
sys.stdout.write('\rPlease wait a moment... ' c)
sys.stdout.flush()
time.sleep(0.1)
最終它不起作用,影片列印錯誤,然后永遠旋轉。這是因為我試圖使用多執行緒然后讓執行緒保持打開狀態嗎?或者,永無止境的加載腳本是一個糟糕的主意嗎?
uj5u.com熱心網友回復:
問題是您將函式放在后臺,然后殺死該函式,而不是在里面運行的命令。
相反,您可以呼叫腳本而不用函式包裝它:
python "/home/solebay/Project/loading_animation.py" &
anim_pid=$!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/422225.html
標籤:
