我在教程中看到的所有程序都是控制臺,代碼從第一行到最后一行執行,如果有的話,一切都從第一行開始。由于某些事件,例如按鍵或代碼中的某些事件,控制臺程序是否有任何方法可以更改其執行方式?我想要做的最好的例子是路由器 CLI。我在哪里可以找到這樣的例子?def main(): while(True): initial_setup() #choose IPs to monitor while(True): do_some_work() # do monitor the IPs我需要一些偵聽器,同時檢測按鍵然后我進入初始設置,同時 do_some_work 工作,并且只有在我完成對 initial_setup do_some_work 的附加更改后才會重新啟動。對不起,我是菜鳥,不太擅長解釋,因為英語不是我的母語。我能說出的現實生活中最好的例子是路由器的 CLI,您可以設置接口,同時路由器在后臺進行路由。塞爾吉奧 S 的代碼:import threadingimport timedef hello(): while(True): print("Hello") time.sleep(2)def hi(): while(True): print("hi") time.sleep(2)def press_key(): a=input() a=False return adef circle(): MrBoolean=True while(MrBoolean): thr=[] thr.append(threading.Thread(target=hello)) thr.append(threading.Thread(target=hi)) thr.append(threading.Thread(target=press_key)) for i in thr: i.start() for i in thr: i.join() mrBoolean=thr[3] def main(): while(True): circle()main()
添加回答
舉報
0/150
提交
取消