Python 执行带线程的函数。带条件的计时器
我想每1秒只执行Python 执行带线程的函数。带条件的计时器,python,python-2.7,python-multithreading,Python,Python 2.7,Python Multithreading,我想每1秒只执行getData函数60次迭代,之后我想执行foo函数 def getDate(): q = client.getQuotes(['EURUSD']) print q 我知道如何每1秒运行一次(使用threading.Timer),但我无法找到如何在某些迭代中执行此操作,也无法等待函数迭代完成Timer.join(),下面是一个如何执行此操作的示例: import threading import time ITERATIONS = 60 # this cou
getData
函数60次迭代,之后我想执行foo
函数
def getDate():
q = client.getQuotes(['EURUSD'])
print q
我知道如何每1秒运行一次(使用threading.Timer),但我无法找到如何在某些迭代中执行此操作,也无法等待函数迭代完成
Timer.join()
,下面是一个如何执行此操作的示例:
import threading
import time
ITERATIONS = 60
# this could be any function
def afunc(t):
# the 't' argument is just to clarify the output
for i in range(2):
print('in timer #' + str(t) + ': ' + str(i))
time.sleep(0.2)
timers = []
for t in range(ITERATIONS):
# create a timer for this iteration (note the interval will be from 1.0 to the number of iterations)
ti = threading.Timer(1.0 + t, afunc, args=[t])
# save the timer in a list
timers.append(ti)
# start the timer
ti.start()
# wait for them all
for ti in timers:
ti.join()
print( 'all finished, call any other method here')
不知道你在问什么。请解释“对于某些迭代,我找不到如何执行”的含义。什么是你不知道怎么做的
it
?您似乎知道如何启动计时器以及如何等待计时器(timer.join()),因此我无法判断您缺少了什么。也许包含更多的代码会更清楚。我想在60次迭代中每1秒运行getDate
函数。我怎样才能运行60次呢?间隔1秒。可能是重复的。非常感谢