我有監視用戶的輸入,看起來像這樣一個主題:如何在Python 3.x中中止用戶的輸入?
def keyboard_monitor(temp): #temp is sys.stdin
global flag_exit
while True:
keyin = temp.readline().rstrip().lower()
if keyin == "exit":
flag_exit = True
print("Stopping...")
if flag_exit == True:
break
如果鍵入退出標誌的設置正確,所有其他線程終止。如果我的另一個線程設置標誌,則此線程拒絕完成,因爲它掛在用戶輸入上。我必須輸入一些東西才能完成。如何更改此代碼,以便在外部設置標誌時程序結束?
開始。 – wim
你可以試試'temp.close()'。不知道可能會有什麼影響。 – meuh
它只需要跨平臺或Unix或Windows? – dawg