【问题标题】:How to return an API response n times after calling it in python?在python中调用它后如何返回n次API响应?
【发布时间】:2021-08-25 03:34:40
【问题描述】:

我一直在使用 python 3x 并制作了如下 API:

@app.route('/testapi', methods=['POST'])
def test_api():
    try:
        response = {'MSG': "Works"}
        return response
    except:
        return False

我一直从 javascript 调用这个 API,一旦我调用 /testapi ,它应该返回响应 n(eg:4) 次,时间延迟为 10 秒。我怎样才能实现这个解决方案?

【问题讨论】:

  • 你在用ajax吗?
  • 是的,正确的@charchit

标签: python python-3.x flask python-requests rest


【解决方案1】:

然后您可以为此使用setIntervalclearInterval

var x = 0;
var intervalID = setInterval(function () {

   // Your logic for calling api here

   if (++x === 4) { // call four times times
       window.clearInterval(intervalID);
   }
}, 10000);// 1 sec == 1000ms

【讨论】:

  • 我期待python中的答案。但这似乎还是不错的!谢谢@charchit。
  • 我认为 python 中没有这样做的东西。也许我错了。
  • 是的,同样的想法。
猜你喜欢
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
相关资源
最近更新 更多