简体   繁体   English

使用线程的连续循环

[英]Continuous loop using threading

I am somewhat new to python. 我对python有点陌生。 I have been trying to find the answer to this coding question for some time. 我已经尝试寻找这个编码问题的答案一段时间了。 I have a function set up to run on a threading timer. 我设置了一个在线程计时器上运行的功能。 This allows it to execute every second while my other code is running. 这使它可以在我的其他代码运行时每秒执行一次。 I would like this function to simply execute continuously, that is every time it is done executing it starts over, rather than on a timer. 我希望此函数简单地连续执行,即每次完成执行后都重新开始,而不是在计时器上执行。 The reason for this is that due to a changing delay in a stepper motor the function takes different amounts of time run. 原因是由于步进电机的延迟变化,该功能需要花费不同的时间运行。

Is this what you're looking for? 这是您要找的东西吗?

from threading import Thread

def f():
    print('hello')

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

Or maybe this, which shows the concurrent paths of execution (for production, remove sleep() calls, of course): 或者,也许这显示了执行的并发路径(对于生产,当然要删除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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM