RUN__IT # python线程池的使用模板

import time
import threading


# 子线程要执行的方法
def test(value1, value2=5):
    print("%s threading is printed %s, %s" % (threading.current_thread().name, value1, value2))
    time.sleep(2)
    return 'finished'


# 回调方法,线程执行完成要执行的方法,此方法必须接受future这个参数,固定写法
def test_result(future):

    print(future.result())
    print(future.running())
    print(1)


if __name__ == "__main__":
    from concurrent.futures import ThreadPoolExecutor
    # threadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")
    threadPool = ThreadPoolExecutor(max_workers=10)
    for i in range(0, 10):
        future = threadPool.submit(test, i, i+2)
        # 不懂这个map和submit的区别
        # future = threadPool.map(test, [i], [i+5])
        future.add_done_callback(test_result)

    threadPool.shutdown(wait=True)


"""
    Future 提供了如下方法:
            cancel():取消该 Future 代表的线程任务。如果该任务正在执行,不可取消,则该方法返回 False;否则,程序会取消该任务,并返回 True。
            cancelled():返回 Future 代表的线程任务是否被成功取消。
            running():如果该 Future 代表的线程任务正在执行、不可被取消,该方法返回 True。
            done():如果该 Funture 代表的线程任务被成功取消或执行完成,则该方法返回 True。
            result(timeout=None):获取该 Future 代表的线程任务最后返回的结果。如果 Future 代表的线程任务还未完成,该方法将会阻塞当前线程,其中 timeout 参数指定最多阻塞多少秒。
            exception(timeout=None):获取该 Future 代表的线程任务所引发的异常。如果该任务成功完成,没有异常,则该方法返回 None。
            add_done_callback(fn):为该 Future 代表的线程任务注册一个“回调函数”,当该任务成功完成时,程序会自动触发该 fn 函数
    """

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是优化后的代码,满足你的要求: ```python import tkinter as tk import subprocess # 创建主窗口 window = tk.Tk() window.title("My App") window.geometry("300x200") # 设置窗口大小 # 添加欢迎标语 welcome_label = tk.Label(window, text="欢迎使用My App!", font=("Arial", 14)) welcome_label.pack(pady=10) # 定义点击运行按钮事件处理函数 def run_program(): # 在这里添加你要运行的程序逻辑 # 调用函数运行程序 start_program() # 定义最小化按钮事件处理函数 def minimize_window(): window.iconify() # 定义启动程序函数 def start_program(): # 在这里添加你要运行的程序逻辑 # 调用subprocess.Popen启动程序 subprocess.Popen(["python", "your_script.py"]) # 创建按钮 button_width = 10 # 按钮宽度 button_height = 2 # 按钮高度 run_button = tk.Button(window, text="运行", width=button_width, height=button_height, command=run_program) run_button.pack() min_button = tk.Button(window, text="最小化", width=button_width, height=button_height, command=minimize_window) min_button.pack() # 运行主循环 window.mainloop() ``` 在上述代码中,我添加了一个欢迎标语,并将其显示在窗口中。在点击运行按钮时,不再直接启动Python脚本,而是调用了一个名为`start_program`的函数,你可以在该函数中添加你要运行的程序逻辑。 你可以根据需要调整代码中的字体、按钮尺寸以及其他界面元素的位置和样式。希望这个优化后的代码满足你的需求!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值