编写一个多线程程序,在输入一个字符串并停止“hello world”输入后,输入另一个字符串,一直重复,直到输入“quit”。
创始人
2024-12-06 08:00:24
0

下面是一个使用多线程编写的程序,可以实现在输入字符串"hello world"后停止,并在输入另一个字符串后重复,直到输入"quit"为止。

import threading

def input_thread():
    while True:
        user_input = input()
        if user_input == "hello world":
            # 通知主线程停止
            main_thread.event.set()
            break
        elif user_input == "quit":
            break

def main_thread_function():
    # 等待输入线程通知
    main_thread.event.wait()

    while True:
        user_input = input()
        if user_input == "quit":
            break

if __name__ == "__main__":
    main_thread = threading.Thread(target=main_thread_function)
    main_thread.event = threading.Event()
    main_thread.start()

    input_thread()
    main_thread.join()

这个程序创建了两个线程:main_threadinput_threadmain_thread是主线程,负责等待输入线程通知后,再循环接收用户输入。input_thread是输入线程,负责循环接收用户输入,并在输入"hello world"时通知主线程停止。

主线程和输入线程之间通过threading.Event()来进行通信。当输入线程接收到"hello world"时,调用main_thread.event.set()来通知主线程停止。主线程在收到通知后,退出循环并结束程序。

请注意,这个程序是无限循环接收用户输入的,直到输入"quit"后才会退出程序。如果你想要在一次输入"hello world"后结束程序,可以根据需要修改代码。

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...