Python Thead.join()仅在所有线程完成后才帮助打印字符串
我目前正在尝试编写一个函数,该函数实现以下功能:Python Thead.join()仅在所有线程完成后才帮助打印字符串,python,python-3.x,multithreading,function,delay,Python,Python 3.x,Multithreading,Function,Delay,我目前正在尝试编写一个函数,该函数实现以下功能: 以随机顺序从“消息”列表中获取所有消息,同时确保没有任何消息重复 在1-10秒范围内的随机延迟秒数后打印 所有线程完成后,它将打印一个字符串“打印完成。您将在5秒钟后返回主菜单。” 然而,我遇到的问题是字符串“Printing is finished…”打印在随机消息之间,而不是在它们之后 我的困惑源于这样一个事实,即.join()方法完全适用于类似的函数。只有在所有线程完成后,才会打印字符串。它们之间的主要区别在于另一个函数不接受任何随机输
- 以随机顺序从“消息”列表中获取所有消息,同时确保没有任何消息重复
- 在1-10秒范围内的随机延迟秒数后打印
- 所有线程完成后,它将打印一个字符串“打印完成。您将在5秒钟后返回主菜单。”
clear()
功能用于在每次输入后清除终端显示
非工作功能:
def option_3():
clear()
messages = ["message_1", "message_2", "message_3", "message_4", "message_5",
"message_6", "message_7", "message_8", "message_9", "message_10"]
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message in messages:
randomized_messages = []
random_index = random.randint(0, len(messages) - 1)
randomized_messages.append(messages.pop(random_index))
for msg in randomized_messages:
t = Thread(target=threading_function, args=(msg, random.choice(range(1,11))))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
def option_2():
global messages_and_times
clear()
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message, seconds in messages_and_times.items():
t = Thread(target=threading_function, args=(message, seconds))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
import os
import random
clear = lambda: os.system('cls')
messages_and_times = {}
def main_menu():
def option_1():
global messages_and_times
clear()
message = input(
"Please type in a message you would like to add to the list:")
clear()
seconds = int(
input("Please type in the time of delay for this message:"))
messages_and_times[message] = seconds
def create_dictionary():
clear()
answer = input(
"Would you like to add another message? (yes/no)").lower()
if answer == "yes":
option_1()
elif answer == "no":
clear()
print("You will now be returned to the main menu.")
time.sleep(1.5)
main_menu()
else:
clear()
print("Please answer yes or no.")
time.sleep(1.5)
create_dictionary()
create_dictionary()
def option_2():
global messages_and_times
clear()
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message, seconds in messages_and_times.items():
t = Thread(target=threading_function, args=(message, seconds))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
def option_3():
clear()
messages = ["message_1", "message_2", "message_3", "message_4", "message_5",
"message_6", "message_7", "message_8", "message_9", "message_10"]
randomized_messages = []
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message in messages:
random_index = random.randint(0, len(messages) - 1)
randomized_messages.append(messages.pop(random_index))
for msg in randomized_messages:
t = Thread(target=threading_function, args=(msg, random.choice(range(1,11))))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
clear()
selection = 0
while selection == 0:
print(("-" * 15) + "MAIN MENU" + ("-" * 15) + "\n")
print("1: Input a message and a corresponding time of delay before its display.")
print("2: Print your customized list of messages.")
print("3: Print random messages with random delays.\n")
selection = int(input(
"Please select one of the options, by typing in the corresponding number:"))
if selection == 1:
option_1()
elif selection == 2:
option_2()
elif selection == 3:
option_3()
else:
clear()
print("Please select from options 1 - 3.\n")
time.sleep(1.5)
main_menu()
工作职能:
def option_3():
clear()
messages = ["message_1", "message_2", "message_3", "message_4", "message_5",
"message_6", "message_7", "message_8", "message_9", "message_10"]
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message in messages:
randomized_messages = []
random_index = random.randint(0, len(messages) - 1)
randomized_messages.append(messages.pop(random_index))
for msg in randomized_messages:
t = Thread(target=threading_function, args=(msg, random.choice(range(1,11))))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
def option_2():
global messages_and_times
clear()
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message, seconds in messages_and_times.items():
t = Thread(target=threading_function, args=(message, seconds))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
import os
import random
clear = lambda: os.system('cls')
messages_and_times = {}
def main_menu():
def option_1():
global messages_and_times
clear()
message = input(
"Please type in a message you would like to add to the list:")
clear()
seconds = int(
input("Please type in the time of delay for this message:"))
messages_and_times[message] = seconds
def create_dictionary():
clear()
answer = input(
"Would you like to add another message? (yes/no)").lower()
if answer == "yes":
option_1()
elif answer == "no":
clear()
print("You will now be returned to the main menu.")
time.sleep(1.5)
main_menu()
else:
clear()
print("Please answer yes or no.")
time.sleep(1.5)
create_dictionary()
create_dictionary()
def option_2():
global messages_and_times
clear()
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message, seconds in messages_and_times.items():
t = Thread(target=threading_function, args=(message, seconds))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
def option_3():
clear()
messages = ["message_1", "message_2", "message_3", "message_4", "message_5",
"message_6", "message_7", "message_8", "message_9", "message_10"]
randomized_messages = []
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message in messages:
random_index = random.randint(0, len(messages) - 1)
randomized_messages.append(messages.pop(random_index))
for msg in randomized_messages:
t = Thread(target=threading_function, args=(msg, random.choice(range(1,11))))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
clear()
selection = 0
while selection == 0:
print(("-" * 15) + "MAIN MENU" + ("-" * 15) + "\n")
print("1: Input a message and a corresponding time of delay before its display.")
print("2: Print your customized list of messages.")
print("3: Print random messages with random delays.\n")
selection = int(input(
"Please select one of the options, by typing in the corresponding number:"))
if selection == 1:
option_1()
elif selection == 2:
option_2()
elif selection == 3:
option_3()
else:
clear()
print("Please select from options 1 - 3.\n")
time.sleep(1.5)
main_menu()
整个主菜单()函数:
def option_3():
clear()
messages = ["message_1", "message_2", "message_3", "message_4", "message_5",
"message_6", "message_7", "message_8", "message_9", "message_10"]
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message in messages:
randomized_messages = []
random_index = random.randint(0, len(messages) - 1)
randomized_messages.append(messages.pop(random_index))
for msg in randomized_messages:
t = Thread(target=threading_function, args=(msg, random.choice(range(1,11))))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
def option_2():
global messages_and_times
clear()
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message, seconds in messages_and_times.items():
t = Thread(target=threading_function, args=(message, seconds))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
import os
import random
clear = lambda: os.system('cls')
messages_and_times = {}
def main_menu():
def option_1():
global messages_and_times
clear()
message = input(
"Please type in a message you would like to add to the list:")
clear()
seconds = int(
input("Please type in the time of delay for this message:"))
messages_and_times[message] = seconds
def create_dictionary():
clear()
answer = input(
"Would you like to add another message? (yes/no)").lower()
if answer == "yes":
option_1()
elif answer == "no":
clear()
print("You will now be returned to the main menu.")
time.sleep(1.5)
main_menu()
else:
clear()
print("Please answer yes or no.")
time.sleep(1.5)
create_dictionary()
create_dictionary()
def option_2():
global messages_and_times
clear()
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message, seconds in messages_and_times.items():
t = Thread(target=threading_function, args=(message, seconds))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
def option_3():
clear()
messages = ["message_1", "message_2", "message_3", "message_4", "message_5",
"message_6", "message_7", "message_8", "message_9", "message_10"]
randomized_messages = []
print("Printing initialized:\n")
def threading_function(message, seconds):
time.sleep(seconds)
print(message)
for message in messages:
random_index = random.randint(0, len(messages) - 1)
randomized_messages.append(messages.pop(random_index))
for msg in randomized_messages:
t = Thread(target=threading_function, args=(msg, random.choice(range(1,11))))
t.start()
t.join()
time.sleep(0.5)
print(
"\nPrinting is finished. You will be returned to the main menu in 5 seconds.")
time.sleep(5)
main_menu()
clear()
selection = 0
while selection == 0:
print(("-" * 15) + "MAIN MENU" + ("-" * 15) + "\n")
print("1: Input a message and a corresponding time of delay before its display.")
print("2: Print your customized list of messages.")
print("3: Print random messages with random delays.\n")
selection = int(input(
"Please select one of the options, by typing in the corresponding number:"))
if selection == 1:
option_1()
elif selection == 2:
option_2()
elif selection == 3:
option_3()
else:
clear()
print("Please select from options 1 - 3.\n")
time.sleep(1.5)
main_menu()
编辑(供将来参考的最终解决方案):
多亏了Almog David的建议,以下是我如何修改函数以使其按预期工作的。我希望这有助于将来有人浏览解决方案
选项2():
选项3():
问题是,您只等待最后一个线程加入(因为循环结束时't'变量保存最后创建的线程对象),所以当最后一个线程完成并且程序继续时,加入函数返回 即使在选项2上,我也能够通过提供以下信息再现此错误:
messages_and_times = {"message_0": 3,
"message_1": 3,
"message_2": 1}
上述配置的结果是:
message_2
Printing is finished. You will be returned to the main menu in 5 seconds.
message_0
message_1
您应该做什么来修复它:
threads = []
for message, seconds in messages_and_times.items():
t = Thread(target=threading_function, args=(message, seconds))
t.start()
threads.append(t)
for t in threads:
t.join()
在非工作版本中,您有一个
t.join()
,它跟在两个嵌套的for
循环之后,循环的最内部有一个t=Thread(…)
,因此执行join()
时t的值是分配给它的最后一个线程。这就是你想要的吗?啊,非常感谢。我还注意到我犯的另一个错误。如果我使用.pop()方法,“消息”列表最终只剩下5个条目,“随机化消息”列表也是如此。我将编辑我的原始帖子以包含最终解决方案。是的,你是对的,我也注意到了这种行为,但我认为这是故意的。