Python Popen() 正在阻塞
Python Popen() is blocking
我想写一个 python 脚本来自动创建 Jekyll 博客,但是 popen()
似乎阻塞并且不能异步调用。
预期的行为是:
- 异步启动
jekyll serve --livereload
- 启动
firefox-esr http://127.0.0.1:4000
异步并等待它(或同步,这与我的用例无关)
- firefox 终止后,Jekyll 也终止。
jekyll = subprocess.Popen(['jekyll', 'serve', '--livereload'])
print('This never gets displayed')
time.sleep(3)
firefox = subprocess.Popen(['firefox-esr', 'http://127.0.0.1:4000'])
firefox.wait()
jekyll.terminate()
但这只会启动 Jekyll 并将其标准输出输出到终端。
这个问题只出现在 Jekyll 中。 ping
或任何其他 command/program 我试过工作正常。
对我做错了什么有什么想法吗?
如果你在 Linux,你可以为它创建一个简单的 bash
脚本。
#!/bin/bash
jekyll serve --livereload &
sleep 5
firefox-esr http://127.0.0.1:4000 &>/dev/null
pid=$(pgrep firefox-esr)
while :
do
sleep 5
if [ -z "$pid" ]
then
pkill ruby 2>/dev/null
echo "Killed jekyll"
break
fi
done
授予此文件执行权限
chmod +x filename.sh
然后 运行 这个 bash
脚本
./filename.sh &
这将使您的脚本 运行 在后台运行。
我想写一个 python 脚本来自动创建 Jekyll 博客,但是 popen()
似乎阻塞并且不能异步调用。
预期的行为是:
- 异步启动
jekyll serve --livereload
- 启动
firefox-esr http://127.0.0.1:4000
异步并等待它(或同步,这与我的用例无关) - firefox 终止后,Jekyll 也终止。
jekyll = subprocess.Popen(['jekyll', 'serve', '--livereload'])
print('This never gets displayed')
time.sleep(3)
firefox = subprocess.Popen(['firefox-esr', 'http://127.0.0.1:4000'])
firefox.wait()
jekyll.terminate()
但这只会启动 Jekyll 并将其标准输出输出到终端。
这个问题只出现在 Jekyll 中。 ping
或任何其他 command/program 我试过工作正常。
对我做错了什么有什么想法吗?
如果你在 Linux,你可以为它创建一个简单的 bash
脚本。
#!/bin/bash
jekyll serve --livereload &
sleep 5
firefox-esr http://127.0.0.1:4000 &>/dev/null
pid=$(pgrep firefox-esr)
while :
do
sleep 5
if [ -z "$pid" ]
then
pkill ruby 2>/dev/null
echo "Killed jekyll"
break
fi
done
授予此文件执行权限
chmod +x filename.sh
然后 运行 这个 bash
脚本
./filename.sh &
这将使您的脚本 运行 在后台运行。