
こんにちは私はレモンバーを作っていますが、プログラムがループとその中で進行するスレッドを管理したいので、Python、Go、C、またはシェルスクリプト以外の言語にしたいと思います。
たとえば、私が呼び出すスクリプトを作成できることがわかりました。ループの繰り返しごとにPythonスクリプトが使用されます。
bar.sh
while true
do
python script.py
sleep 1
done
以来script.py
print('%{c}hello')
だから私はこのように実行
sh bar.sh | lemonbar
これは効果があり、私はhello
バーの中央に1つを得ました。しかし、私はこのようなことをしたいです。
bar.py
while True:
print('%{c}hello')
レモンバーにパイプで接続
python bar.py | lemonbar
これはうまくいきません。バーがありますが、そこには何もありません。
私の考えでは、これは私のシェルで使用されているファイル記述子と比較して、Python印刷機能で使用されるファイル記述子に関連しているようです。zsh
編集する:私も試しました
import sys, time
fd = sys.stdout
while True:
fd.write('hej\n')
time.sleep(1)
それは何も変えません。
私の質問を読んでくれてありがとう。お役に立てば幸いです:)
答え1
実際、誰かの実装を読んでこれを行う方法を見つけました。レモンバーモジュール
import time
from subprocess import Popen, PIPE
fd = Popen('lemonbar', stdin=PIPE, stdout=PIPE, encoding='UTF-8')
while True:
time.sleep(1)
fd.stdin.write('%{c}hello')
fd.stdin.flush()
print(fd.stdout.read())
秘密は、ファイル記述子を作成してフラッシュすることです。
だから私もできます。
bar.py
import time
import sys
fd = sys.stdout
while True:
fd.write("%{c}hello")
fd.flush()
time.sleep(1)
その後、通常どおり実行します。
python bar.py | lemonbar