-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfanout.py
46 lines (35 loc) · 1.13 KB
/
fanout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import httpx
import dispatch
@dispatch.function
def get_repo(repo_owner: str, repo_name: str):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}"
api_response = httpx.get(url)
api_response.raise_for_status()
repo_info = api_response.json()
return repo_info
@dispatch.function
def get_stargazers(repo_info):
url = repo_info["stargazers_url"]
response = httpx.get(url)
response.raise_for_status()
stargazers = response.json()
return stargazers
@dispatch.function
async def reduce_stargazers(repos):
result = await dispatch.gather(*[get_stargazers(repo) for repo in repos])
reduced_stars = set()
for repo in result:
for stars in repo:
reduced_stars.add(stars["login"])
return reduced_stars
@dispatch.function
async def fanout():
# Using gather, we fan-out the following requests:
repos = await dispatch.gather(
get_repo("dispatchrun", "coroutine"),
get_repo("dispatchrun", "dispatch-py"),
get_repo("dispatchrun", "wzprof"),
)
return await reduce_stargazers(repos)
if __name__ == "__main__":
print(dispatch.run(fanout()))