使用线程连续循环

时间:2013-07-26 12:15:40

标签: python multithreading loops timer multiprocessing

我对python有些新意。我一直试图找到这个编码问题的答案。我有一个功能设置为在线程计时器上运行。这允许它在我的其他代码运行时每秒执行一次。我希望这个函数能够简单地连续执行,也就是每次执行它时都会重复执行,而不是在计时器上执行。原因在于,由于步进电机的延迟变化,该功能需要不同的时间运行。

1 个答案:

答案 0 :(得分:0)

这是你要找的吗?

from threading import Thread

def f():
    print('hello')

while True:
    t = Thread(target=f)
    t.start()
    t.join()

或许这可以显示并发执行路径(对于生产,当然要删除sleep()次调用):

from threading import Thread
from time import sleep

def g():
    print('hello')
    sleep(1)

def f():
    while True: g()

Thread(target=f).start()
sleep(1)
print('other code here')