我试图每秒运行一个函数“foo”。我要说几分钟。在
函数foo()向服务器发出100个HTTP请求(其中包含一个JSON对象),并打印JSON响应。在
简而言之,我必须在5分钟内每秒发出100个HTTP请求。在
我刚开始学习python,知识面不广。这就是我所尝试的:import threading
noOfSecondsPassed = 0
def foo():
global noOfSecondsPassed
# piece of code which makes 100 HTTP requests (I use while loop)
noOfSecondsPassed += 1
while True:
if noOfSecondsPassed < (300) # 5 minutes
t = threading.Timer(1.0, foo)
t.start()
由于有多个线程,foo函数没有被调用300次,但远不止这些。
我也试过设置一个锁:
^{pr2}$
其余代码与前面的代码片段相同。但这也行不通。在
我该怎么做?在
编辑:不同的方法
我尝试过这种方法,这对我很有效:def foo():
noOfSecondsPassed = 0
while noOfSecondsPassed < 300:
#Code to make 100 HTTP requests
noOfSecondsPassed +=1
time.sleep(1.0)
foo()
这样做有什么坏处吗?在