【问题标题】:contents in python print function is not printed out [duplicate]python打印功能中的内容没有打印出来[重复]
【发布时间】:2018-07-21 11:35:38
【问题描述】:
def recv():

while True:
    data = udp_socket.recvfrom(1024)
    content, address = data
    ip, port = address
    print('\r>>>[%s %d] %s' % (ip, port, content.decode()))
    print('<<<', end='')

当我调用这个函数时,最后一个print函数中的内容(最后一行代码)没有打印出来。有人知道为什么吗?谢谢!

【问题讨论】:

    标签: python python-3.x function printing


    【解决方案1】:

    标准输出流被缓冲,因此&lt;&lt;&lt; 很可能在缓冲区中,并且会在下次写入换行符时被刷新。尝试删除end=''。你也可以切换到使用sys.stderr,它(我相信)是无缓冲的。

    【讨论】:

    • 为什么要缓冲?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多