Exécutez actuellement les trois commandes suivantes dans le terminal bash?:
1?:php test.php >> test.log
2?:python test.py >> test.log
3?:sh test.sh >> test.log
Et exécutez dans une autre fenêtre?:tail -f test.log
1 php et 3 bash peuvent être écrits dans test.log en temps réel,
Mais pourquoi Python n'écrit-il dans test.log qu'après la fin du programme Python??
Et comme ceci?:
echo "cd $test_dir && /usr/bin/python test.py &" >> test_py.sh && sh test_py.sh
Mettre Python dans un script sh ne fonctionnera pas non plus
Pièce jointe?:
1 test.php
<?php
$je = 1?;
tandis que (Vrai) {
dormir(1);
imprimer $i++ . "\r\n";
si ($i > 10) {
casser;
}
}
?>
2 test.py
#!/usr/bin/python
#codage=utf-8
heure d'importation
je = 1
tandis que Vrai?:
je += 1
imprimer je
temps.sommeil(1)
si je > 10?:
casser
imprimer "--------------fin---------------"
3 test.sh
#!/usr/bin/env bash
# cd /Utilisateurs/cg/MesFichiers/test && /usr/bin/python cgcg.py &
je = 1
tandis que [[ 1 ]];
dormir 1
je=`expr $i + 1`
écho $i
fait
Je suis curieux, pourquoi PHP n'effectue-t-il pas de mise en mémoire tampon de bloc dans ce cas??
Python Si vous souhaitez voir immédiatement la sortie standard des appareils autres que le terminal, vous pouvez soit la vider vous-même (print(..., flush=True)
ou utiliser sys en Python 2 .stdout.flush
), soit utilisez python -u
pour exécuter le script, soit remplacez sys.stdout
. print(..., flush=True)
或者 Python 2 里用 sys.stdout.flush
),要么使用 python -u
來(lái)運(yùn)行腳本,要么你去把 sys.stdout
替換掉。
通常,對(duì)標(biāo)準(zhǔn)輸出(以及其它除標(biāo)準(zhǔn)錯(cuò)誤之外的文件)的輸出,默認(rèn)是帶緩沖的,你可以看一下這個(gè)教程。對(duì)于終端是行緩沖,對(duì)于其它設(shè)備是塊緩沖。標(biāo)準(zhǔn)錯(cuò)誤一般是無(wú)緩沖的。
如果要寫日志,或者做基于行的持續(xù)性輸出的話,記得設(shè)置相應(yīng)的緩沖模式(open
函數(shù)的 buffer 參數(shù)),或者在合適的地方調(diào)用 .flush()
open
), ou d'appeler .flush() dans le fichier approprié. méthode place
. ??