用户输入而不会延迟程序

时间:2018-10-22 15:46:07

标签: python-3.x server console

我正在制作一个列出客户端消息的服务器程序,但是,我需要实现一个服务器“控制台”,该服务器可以打印并使用用户输入作为命令 。但是,python内置的input()不会让print()将数据输出到py控制台。

## code example
import time

while true:
    print('Working')
    time.sleep(0.1) # so doesn't crash
    var = input('Delays prog')

但是我的问题是,input()函数会中断线程并在python控制台上正常打印数据

像这样:

============= RESTART: /home/pi/Desktop/Pythonic/Chat/Server.py =============
Server name : serv
Server running on localhost at port 5386
> Connected To
 ('127.0.0.1', 43686)
00:58:31 : [Guest]Owner: Hello

令人讨厌的代码:

host = 'localhost'
port = random.randint(5000,6000)
user = 'No one'

name = input('Server name : ')

server = Server(host, port, user)

clients = {}
threads = []

while True:
    client, caddr = server.socket.accept()
    # name extraction
    name = client.recv(1024)
    #name = getname(caddr)

    clients[client] = name.decode('utf-8')

    thread = threading.Thread(target=recv_loop, args=(server,client, caddr))
    thread.start()
    action = input('> ')

0 个答案:

没有答案