在上面的示例中,我们创建了一个线程对象thread,并将my_thread函数作为目标传递给它。在my_thread函数中,我们使用time.sleep(2)来暂停线程的执行2秒钟。在主线程中,我们等待线程完成,并在线程完成后打印出一条消息。 使用threading.Timer 类 除了time.sleep()函数外,Python还提供了threading.Timer类来实现线程的延时。
在Python中,可以使用concurrent.futures.ThreadPoolExecutor来创建线程池,并通过自定义回调函数在每10个任务完成后添加延时。以下是一个示例代码: 代码语言:txt 复制 import time from concurrent.futures import ThreadPoolExecutor, as_completed def task(n): print(f"Task {n} started") time.sleep(...