
想要在 python 中让任务间隔一段时间运行,而不影响其他任务,可以利用多线程编程技术。
考虑以下代码示例:
import threading
import time
# 创建一个线程,每分钟运行一次指定的任务
def task_1():
while True:
# 执行待完成的任务
pass
time.sleep(60)
# 创建并启动线程
thread_1 = threading.Thread(target=task_1)
thread_1.start()
# 主线程中继续执行其他任务,不受线程 1 影响
while True:
# 执行待完成的任务
pass在这段代码中,我们创建了一个 task_1() 函数,它定义了需要每分钟运行的任务。然后,我们创建了一个 thread_1 线程并将其设置为在后台运行 task_1() 函数。主线程将继续执行其他任务,不受线程 1 的影响。










