我正在尝试编写一个简单的
Python IRC客户端.到目前为止,我可以读取数据,如果自动化,我可以将数据发送回客户端.我在一段时间内得到数据为True,这意味着我无法在读取数据的同时输入文本.如何在控制台中输入文本,只有在按Enter键时才会发送文本,同时运行无限循环?
基本代码结构:
while True:
read data
#here is where I want to write data only if it contains '/r' in it
最佳答案 另一种方法涉及线程.
import threading
# define a thread which takes input
class InputThread(threading.Thread):
def run(self):
self.daemon = True
while True:
self.last_user_input = input('input something: ')
# do something based on the user input here
# alternatively, let main do something with
# self.last_user_input
# main
it = InputThread()
it.start()
while True:
# do something
# do something with it.last_user_input if you feel like it