In my multi-threaded (2 threads) python program, both threads writes to the console. Main thread accepts commands using input()
, another thread fetches some data from server and prints some messages. I have managed to move the input prompt (first thread) to new line by printing '\r'
to the console. But I can't find a way to bring back the input that was on half way with that prompt. How can I achieve the correct behavior.
This is a snippet of code from the program:
class GetServerData:
def mainthread(self, interval):
updates = threading.Thread(target=self.getUpdates, args={interval})
updates.start()
self.threadsrunning = True
self.continuemainloop = True
while(self.continuemainloop):
self.promptinput()
command = input()
if(command == "")
self.promptinput()
elif(command=="quit")
self.continueloop = False
print("\rWaiting for all threads to terminate ...")
while(self.threadsrunning)
pass
self.continuemainloop = False
print("Program terminated")
def getUpdates(self, interval)
self.continueloop = True
while(self.continueloop):
#Code for getting data from server
#and print/process the response
#if server responds
time.sleep(15)
print("\rReceived Updates ...") #This prints at the current line
#After this, prompt should be shown
#If something was being typed, it should also be shown here
self.promptinput()
self.threadsrunning = False
def promptinput(self):
print(" >> ", end='', flush=True) #Prompt shows without moving to next line
#TODO : Print current keyboard input which is not finished yet
What should I put after the prompt prints >>
, to show unfinished input.