python的print输出缓冲区问题

python的print输出缓冲区问题

切记切记切记!!!
调试过程中使用print函数时,如因死循环或者某种原因卡住,记得在print函数的下一行加一句
sys.stdout.flush() (记得先import sys 模块)

#!/usr/bin/python
#coding=utf-8
'''
暂停1s输出
'''

import time

def printStar(n):
        for i in range(n):
                print " * ",
                time.sleep(1)

if __name__ == '__main__':
        printStar(10)

等待10s后一次性输出:

  * * * * * * * * * *
#!/usr/bin/python
#coding=utf-8
'''
暂停1s输出
'''

import time
import sys


def printStar(n):
        for i in range(n):
                print " * ",
                sys.stdout.flush()
                time.sleep(1)

if __name__ == '__main__':
        printStar(10)


则是一秒钟输出一个*

你可能感兴趣的:(python,开发语言,后端)