{"id":34048326,"url":"https://github.com/aarondwi/singleflight","last_synced_at":"2026-04-01T20:45:01.863Z","repository":{"id":57467862,"uuid":"266268894","full_name":"aarondwi/singleflight","owner":"aarondwi","description":"Coalesce multiple identical calls into one, preventing thundering-herd/stampede to database/other backends","archived":false,"fork":false,"pushed_at":"2022-01-27T03:46:50.000Z","size":27,"stargazers_count":14,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-02T20:56:07.535Z","etag":null,"topics":["cache","cache-stampede","python","singleflight","thundering-herd"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aarondwi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-23T05:23:18.000Z","updated_at":"2025-10-30T15:19:29.000Z","dependencies_parsed_at":"2022-09-19T09:01:51.635Z","dependency_job_id":null,"html_url":"https://github.com/aarondwi/singleflight","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aarondwi/singleflight","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aarondwi%2Fsingleflight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aarondwi%2Fsingleflight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aarondwi%2Fsingleflight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aarondwi%2Fsingleflight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aarondwi","download_url":"https://codeload.github.com/aarondwi/singleflight/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aarondwi%2Fsingleflight/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31291790,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-01T13:12:26.723Z","status":"ssl_error","status_checked_at":"2026-04-01T13:12:25.102Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","cache-stampede","python","singleflight","thundering-herd"],"created_at":"2025-12-14T00:04:02.712Z","updated_at":"2026-04-01T20:45:01.856Z","avatar_url":"https://github.com/aarondwi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# singleflight\n\n[![Build Status](https://travis-ci.org/aarondwi/singleflight.svg?branch=master)](https://travis-ci.org/aarondwi/singleflight)\n\nCoalesce multiple identical call into one, preventing thundering-herd/stampede to database/other backends\n\nIt is a python port of [golang's groupcache singleflight implementation](https://github.com/golang/groupcache/blob/master/singleflight/singleflight.go)\n\nThis module **does not** provide caching mechanism. Rather, this module can used behind a caching abstraction to deduplicate cache-filling call\n\nOnly support python 3.5+\n\nInstallation\n-----------------------\n\n```python\npip install singleflight\n```\n\nUsage\n-----------------------\n\nThis modules has 3 implementation, which can be imported as follows\n\n```python\nfrom singleflight.basic import SingleFlight # for multi-threaded apps\nfrom singleflight.gevent import SingleFlightGevent as SingleFlight # for gevent apps\nfrom singleflight.asynchronous import SingleFlightAsync as SingleFlight # for asyncio/curio apps\n```\n\nThen you can use it as follows (the example shows the multi-threaded version). Note that, `key` is important for the modules to know which call should be de-duplicated\n\n```python\nfrom time import sleep\nfrom concurrent.futures import ThreadPoolExecutor\nfrom functools import partial\n\nfrom singleflight.basic import SingleFlight\n\nif __name__ == '__main__':\n  sf = SingleFlight()\n  executor = ThreadPoolExecutor(max_workers=10)\n\n  counter = 0\n  result = \"this is the result\"\n  def work(num):\n    global counter, result\n    sleep(0.1) # emulate bit slower call\n    counter += 1\n    return (result, num)\n\n  res = []\n  for i in range(10):\n    sfc = partial(sf.call, work, \"key\", i+1)\n    r = executor.submit(sfc)\n    res.append(r)\n\n  for r in res:\n    assert r.result()[0] == result\n    # because only the first one can get the lock\n    # and only that one request call\n    assert r.result()[1] == 1\n  \n  assert counter == 1\n```\n\nFor decorator fans, you can also use it to wrap your function.\n\n```python\nfrom time import sleep\nfrom concurrent.futures import ThreadPoolExecutor\nfrom functools import partial\n\nfrom singleflight.basic import SingleFlight\n\nif __name__ == '__main__':\n  sf = SingleFlight()\n  executor = ThreadPoolExecutor(max_workers=10)\n\n  # success case\n  counter = 0\n  result = \"this is the result\"\n\n  @sf.wrap\n  def work(num):\n    global counter, result\n    sleep(0.1) # emulate bit slower call\n    counter += 1\n    return (result, num)\n\n  res = []\n  for i in range(10):\n    sfc = partial(work, \"key\", i+1)\n    r = executor.submit(sfc)\n    res.append(r)\n\n  for r in res:\n    assert r.result()[0] == result\n    # because only the first one can get the lock\n    # and only that one request call\n    assert r.result()[1] == 1\n  \n  assert counter == 1\n```\n\nAll `*args` and `**kwargs` your function has is passed directly later. Exceptions are also raised normally.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faarondwi%2Fsingleflight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faarondwi%2Fsingleflight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faarondwi%2Fsingleflight/lists"}