php - Ask about a Linux process output redirection problem?
过去多啦不再A梦
过去多啦不再A梦 2017-05-16 13:08:29
0
1
434

Currently execute the following three commands in the bash terminal:
1:
php test.php >> test.log

2:
python test.py >> test.log

3:
sh test.sh >> test.log

And execute in another window:
tail -f test.log

1 php and 3 bash can be written in test.log in real time,

But why does python only write in test.log after the python program ends?
And like this:

echo "cd $test_dir && /usr/bin/python test.py &" >> test_py.sh && sh test_py.sh

Putting python into an sh script won’t work either

Attachment:
1 test.php

<?php
    $i = 1;
    while (True) {
        sleep(1);
        print $i++ . "\r\n";
        if ($i > 10) {
            break;
        }
    }
?>

2 test.py

#!/usr/bin/python
#coding=utf-8

import time

i=1
while True:
    i += 1
    print i
    time.sleep(1)
    if i > 10:
        break
print "----------end-----------"

3 test.sh

#!/usr/bin/env bash
# cd /Users/cg/MyFiles/test && /usr/bin/python cgcg.py &
i=1
while [[ 1 ]]; do
    sleep 1
    i=`expr $i + 1`
    echo $i
done
过去多啦不再A梦
过去多啦不再A梦

reply all(1)
phpcn_u1582

I am curious, why does PHP not perform block buffering in this case?

Python If you want to immediately see the standard output of devices other than the terminal, you can either flush it yourself (print(..., flush=True) 或者 Python 2 里用 sys.stdout.flush),要么使用 python -u 来运行脚本,要么你去把 sys.stdout replace it.

Usually, the output to standard output (and other files except standard error) is buffered by default. You can take a look at this tutorial. Line buffering for terminals and block buffering for other devices. Standard error is generally unbuffered.

If you want to write logs or do line-based continuous output, remember to set the corresponding buffering mode (open 函数的 buffer 参数),或者在合适的地方调用 .flush() method.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template