【问题标题】:How can you speed up repeated API calls?如何加快重复的 API 调用?
【发布时间】:2020-07-30 05:44:18
【问题描述】:

为了详细了解,我附上了文件链接: Actual file

我在列表中有数据,其语法类似:

i = [a.b>c.d , e.f.g>h.i.j ]

例如列表的每个元素都有多个用“.”分隔的子元素。和“>”。

 for i in range(0, len(l)):
    reac={}
    reag={}
    t = l[i].split(">")
    REAC = t[0]
    Reac = REAC.split(".")

    for o in range(len(Reac)):

        reaco = "https://ai.chemistryinthecloud.com/smilies/" + Reac[o]

        respo = requests.get(reaco)
        reac[o] ={"Smile":Reac[o],"Details" :respo.json()}

    if (len(t) != 1):
        REAG = t[1]
        Reag = REAG.split(".")
        for k in range(len(Reag)):

            reagk = "https://ai.chemistryinthecloud.com/smilies/" + Reag[k]
            repo = requests.get(reagk)
            reag[k] = {"Smile": Reag[k], "Details" :repo.json()}
        res = {"Reactants": list(reac.values()), "Reagents": list(reag.values())}
        boo.append(res)
    else:
        res = {"Reactants": list(reac.values()), "Reagents": "No reagents"}
        boo.append(res)

我们已经分离了所有元素,并且对于每个元素,我们都调用了 3rd 方 API。这太浪费时间了。

有什么办法可以减少这个时间并针对循环进行优化?

回复大约需要 1 分钟。我们希望优化到 5-10 秒。

【问题讨论】:

  • 如果你想减少 API 调用的数量,你可能只能通过缓存结果来做到这一点(例如,不要调用 K 两次),并通过并行运行(线程很好) .您的循环几乎肯定不会对任何事情产生影响。
  • 但问题在于时间复杂度。它具有 MxN 时间复杂度,并且 API 调用也需要额外的时间。我们想减少它。
  • 你能帮忙实现线程吗?
  • 没有O(m*n)复杂度,你有O(k)复杂度。您对字符串中的每个元素进行 api 调用,拆分和循环具有“字面上”的零影响。我的建议是对字符串中的每个唯一元素并行执行一次 api 调用,这是您最终可以做到的最快速度。如果你持久化缓存,它甚至会让你的计算瞬间完成。
  • 你能帮我写代码吗@cireo

标签: python api for-loop


【解决方案1】:

只需这样做。我更改了您的输出格式,因为它使事情变得不必要地复杂,并删除了您正在做的许多非 Python 的事情。第二次运行应该是即时的,如果您在方程式或反应的两侧有重复的值,第一次应该更快。

# pip install cachetools diskcache
from cachetools import cached
from diskcache import Cache

BASE_URL = "https://ai.chemistryinthecloud.com/smilies/"
CACHEDIR = "api_cache"

@cached(cache=Cache(CACHEDIR))
def get_similies(value):
    return requests.get(BASE_URL + value).json()


def parse_eq(eq):
    reac, _, reag = (set(v.split(".")) for v in eq.partition(">"))
    return {
        "reactants": {i: get_similies(i) for i in reac},
        "reagents": {i: get_similies(i) for i in reag}
    }

result = [parse_eq(eq) for eq in eqs]

【讨论】:

  • 它正在工作,但是当时列表中的值是空的,代码被破坏了有什么建议吗?
  • rdkit 多次尝试安装,所以我不打算运行代码。你可以为你的新问题打开一个新问题,或者提供信息吗? “代码被破坏”没有任何意义
  • 好的,我会为 @Cireo 提出新问题
猜你喜欢
  • 1970-01-01
  • 2018-09-11
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
相关资源
最近更新 更多