Maison > Questions et réponses > le corps du texte
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
phpcn_u15822017-05-16 13:10:29
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
来运行脚本,要么你去把 sys.stdout
替换掉。
通常,对标准输出(以及其它除标准错误之外的文件)的输出,默认是带缓冲的,你可以看一下这个教程。对于终端是行缓冲,对于其它设备是块缓冲。标准错误一般是无缓冲的。
如果要写日志,或者做基于行的持续性输出的话,记得设置相应的缓冲模式(open
函数的 buffer 参数),或者在合适的地方调用 .flush()
open
), ou d'appeler .flush() dans le fichier approprié. méthode place
. 🎜