Python如何打印而不会覆盖我们在多线程时输入的内容

时间:2018-06-14 16:16:58

标签: python python-3.x code-formatting

我尝试使用python进行聊天,但如果对方在输入内容时发送文字,那就搞砸了。我的代码是这样的:

import threading
import time
import sys

def printf(str, *args):
    print(str % args, end='')

def printwait():
    global End
    while not End:
        time.sleep(3)
        print(' ')
        print('waiting respond')


def reqinput():
    global End
    while not End:
        name = input("Input your name:")
        End=name=='@end'

End=False

t1=threading.Thread(target=printwait)
t2=threading.Thread(target=reqinput)

t1.start()
t2.start()

t1.join()
t2.join()

print('done')

当我在打字中间然后睡眠时间到期时,它就像这样搞砸了:

Input your name:waiting respond
im trying twaiting respond
o typwaiting respond
e somethingwaiting respond
 here
Input your name:waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
@waiting respond
end
waiting respond
done

虽然我希望它只是打印等待响应'在上面一行,或者只是在等待回复之后移动我想要输入的内容

0 个答案:

没有答案