jupyter notebook中 后台执行bash卡住解决

jupyter版本

!jupyter --version

Selected Jupyter core packages...
IPython          : 8.27.0
ipykernel        : 6.29.5
ipywidgets       : 8.1.5
jupyter_client   : 8.6.2
jupyter_core     : 5.7.2
jupyter_server   : 2.14.2
jupyterlab       : 4.2.5
nbclient         : 0.10.0
nbconvert        : 7.16.4
nbformat         : 5.10.4
notebook         : 7.2.2
qtconsole        : not installed
traitlets        : 5.14.3
Selection deleted

jupyter如何判定%%bash块是否执行完毕

若该%%bash块中启动的存在进程的 stdoutstderr 输出 到该bash进程自身,

则jupyter认为该块没有执行完毕,要等待该块执行完毕,

这样会导致卡在该块,无法执行后续块

解决办法是 让该%%bash块中, 业务进程 的stdoutstderr 重定向 到其他文件,

注意 %%bash块中 书写的 后台执行业务脚本.sh & , jupyter并不认为他已经执行完了

(举例)jupyter中启动uiauto.dev的网页服务

%%bash 

#杀死python进程 uiauto.dev 
kill $(ps auxf | grep uiauto.dev  | grep -v grep | tr -s ' ' | cut -d ' ' -f 2)
sleep 1

source /app2/pyAui/.venv/bin/activate ; 
set -x;  
cur_pid=$$

uiauto_booted=false
ps auxf | grep uiauto | grep -v grep && uiauto_booted=true 

if ! $uiauto_booted ; then
#启动uiauto.dev的网页服务 (将stdout、stderr脱离当前bash进程,以欺骗jupyter使其认为该进程已经执行完毕)
uiauto.dev 1>/tmp/uiauto.dev.stdout 2>/tmp/uiauto.dev.stderr &   
uiauto_dev_pid=$!
sleep 2; cat /tmp/uiauto.dev.*
fi


点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部