我能够提供的所有示例都没有真正解决我的问题,即在后台不断循环执行某个过程,而程序的其余部分继续运行。
以下是一个使用_thread方法的简单示例:
import _thread
import time
def countSeconds():
time.sleep(1)
print("Second")
_thread.start_new(countSeconds, ())
def countTenSeconds():
time.sleep(10)
print("Ten seconds passed")
_thread.start_new(countTenSeconds, ())
_thread.start_new(countSeconds, ())
_thread.start_new(countTenSeconds, ())
忽略我们可以追踪秒数的明显事实,如果它是十的倍数,则打印不同的东西,我该如何更有效地创建这个程序。
在我的实际程序中,线程似乎在消耗内存,我假设是由于创建多个线程实例而导致的。我需要在每个过程的结尾处“start_new”线程吗?
感谢任何帮助。