我正在编写一个程序,它将根据需求从数据中选择数据。我希望能够随时更新这些要求。但是,当我添加简单菜单时,其他线程不起作用。它只是在开始时打印“测试”功能。
def print_menu():
print("1 - Pairs requirements update")
print("2 - Favorite pairs update")
def menu_loop():
while True:
print_menu()
choice = int(input("Enter your choice [1-2]: "))
if choice == 1:
requirements()
elif choice==2:
get_favorite()
else:
raw_input("Wrong option selection. Enter any key to try again..")
def print_results():
while True:
print('Start printing test')
time.sleep(1)
threads = [threading.Thread(target=menu_loop),
threading.Thread(target=print_results)]
[thread.start() for thread in threads]
[thread.join() for thread in threads]
这里是怎么回事,以及在菜单运行时始终如何打印测试?
编辑(解决方案): 我已经使用键盘库更改了菜单循环,并且可以唤醒,但是不知道它是否是最佳选择。
def menu_loop():
while True:
try:
if keyboard.is_pressed('q'):
while True:
print_menu()
choice = int(input("Enter your choice [1-3]: "))
if choice == 1:
requirements()
elif choice==2:
get_favorite()
elif choice==3:
break
else:
raw_input("Wrong option selection. Enter any key to try again..")
else:
pass
except:
break