Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用线程的连续循环_Python_Multithreading_Loops_Timer_Multiprocessing - Fatal编程技术网

Python 使用线程的连续循环

Python 使用线程的连续循环,python,multithreading,loops,timer,multiprocessing,Python,Multithreading,Loops,Timer,Multiprocessing,我对python有些陌生。一段时间以来,我一直在试图找到这个编码问题的答案。我有一个函数设置为在线程计时器上运行。这允许它在其他代码运行时每秒执行一次。我希望这个函数能够连续执行,也就是说,每次完成后,它都会重新开始执行,而不是在计时器上执行。原因是,由于步进电机的延迟变化,该功能需要不同的时间运行。这就是您想要的吗 from threading import Thread def f(): print('hello') while True: t = Thread(targ

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

这就是您想要的吗

from threading import Thread

def f():
    print('hello')

while True:
    t = Thread(target=f)
    t.start()
    t.join()
或者,这显示了并发执行路径(对于生产,当然要删除
sleep()
调用):


你能展示一下你写的代码吗?如果我们能看到您已经尝试过的内容,那么提供指导就更容易了。如果您希望函数持续执行,为什么不编写函数,让它重新开始呢?然后在一个新线程中启动它一次,它将在该线程中继续运行?@vik:如果你是说递归地调用它自己,那么你就达到了递归深度限制,即
RuntimeError:maximum recursion depth excelled
我明白为什么我是指递归,我的意思更像是写它,这样它将只是inf循环或什么的,并一遍又一遍地重复任务,而不是不断地让外部的东西一遍又一遍地调用它。如果你想在函数调用之间尽可能没有时间延迟,我不明白为什么这比只使用函数循环更可取。也许他正在寻找一种不使用无限循环的解决方案。
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')