我有一个大约需要 15 分钟才能运行的脚本。它基本上命中了一堆 api 端点。因为我们的一些生产服务在托管这些端点的地方运行,所以我想限制我的脚本(每周运行一次),这样它就不会对我们的系统产生太大的影响。最简单的方法是什么?该脚本主要用于循环。我应该只添加一些 sleep 语句还是有更标准化的方法来做到这一点?我不在乎脚本是否需要很长时间才能运行,因为我会通宵运行它。
for i in list: <-big list
make_call_to_api() <-typically couple seconds
time.sleep(5) <-sleep 5 seconds
最佳答案
我认为使用 time 没问题。 但是您也可以使用 threading.Timer :
from threading import Timer
def make_api_with_interval(*arg, **kwargs):
# BODY of make api call
make_call_to_api()
Timer(30.0, make_api_with_interval, ["args"], {'kwarg': 'kwarg'}).start()
Timer(30.0, make_api_with_interval).start() # after 30 seconds, api call would be performed.
关于python - 如何礼貌地进行大量的api调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35908786/