Skip to content

Commit bb6ec79

Browse files
Merge pull request #176 from dispatchrun/aiohttp-2
interoperability with asyncio (part 3): refactor with aiohttp
2 parents 4a344ad + 41c6ad3 commit bb6ec79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2579
-3465
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ import dispatch
107107
def greet(msg: str):
108108
print(f"Hello, ${msg}!")
109109

110-
dispatch.run(lambda: greet.dispatch('World'))
110+
dispatch.run(greet('World'))
111111
```
112112

113113
Obviously, this is just an example, a real application would perform much more

examples/auto_retry.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
3+
import requests
4+
5+
import dispatch
6+
import dispatch.integrations.requests
7+
8+
rng = random.Random(2)
9+
10+
11+
def third_party_api_call(x):
12+
# Simulate a third-party API call that fails.
13+
print(f"Simulating third-party API call with {x}")
14+
if x < 3:
15+
print("RAISE EXCEPTION")
16+
raise requests.RequestException("Simulated failure")
17+
else:
18+
return "SUCCESS"
19+
20+
21+
# Use the `dispatch.function` decorator to declare a stateful function.
22+
@dispatch.function
23+
def auto_retry():
24+
x = rng.randint(0, 5)
25+
return third_party_api_call(x)
26+
27+
28+
if __name__ == "__main__":
29+
print(dispatch.run(auto_retry()))

examples/auto_retry/__init__.py

Whitespace-only changes.

examples/auto_retry/app.py

-64
This file was deleted.

examples/auto_retry/test_app.py

-51
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
"""Fan-out example using the SDK gather feature
2-
3-
This example demonstrates how to use the SDK to fan-out multiple requests.
4-
5-
Run with:
6-
7-
uvicorn fanout:app
8-
9-
10-
You will observe that the get_repo_info calls are executed in parallel.
11-
12-
"""
13-
141
import httpx
15-
from fastapi import FastAPI
16-
17-
from dispatch import gather
18-
from dispatch.fastapi import Dispatch
192

20-
app = FastAPI()
21-
22-
dispatch = Dispatch(app)
3+
import dispatch
234

245

256
@dispatch.function
26-
async def get_repo(repo_owner: str, repo_name: str):
7+
def get_repo(repo_owner: str, repo_name: str):
278
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}"
289
api_response = httpx.get(url)
2910
api_response.raise_for_status()
@@ -32,7 +13,7 @@ async def get_repo(repo_owner: str, repo_name: str):
3213

3314

3415
@dispatch.function
35-
async def get_stargazers(repo_info):
16+
def get_stargazers(repo_info):
3617
url = repo_info["stargazers_url"]
3718
response = httpx.get(url)
3819
response.raise_for_status()
@@ -42,7 +23,7 @@ async def get_stargazers(repo_info):
4223

4324
@dispatch.function
4425
async def reduce_stargazers(repos):
45-
result = await gather(*[get_stargazers(repo) for repo in repos])
26+
result = await dispatch.gather(*[get_stargazers(repo) for repo in repos])
4627
reduced_stars = set()
4728
for repo in result:
4829
for stars in repo:
@@ -52,18 +33,14 @@ async def reduce_stargazers(repos):
5233

5334
@dispatch.function
5435
async def fanout():
55-
# Using gather, we fan-out the four following requests.
56-
repos = await gather(
36+
# Using gather, we fan-out the following requests:
37+
repos = await dispatch.gather(
5738
get_repo("dispatchrun", "coroutine"),
5839
get_repo("dispatchrun", "dispatch-py"),
5940
get_repo("dispatchrun", "wzprof"),
6041
)
61-
62-
stars = await reduce_stargazers(repos)
63-
print("Total stars:", len(stars))
42+
return await reduce_stargazers(repos)
6443

6544

66-
@app.get("/")
67-
def root():
68-
fanout.dispatch()
69-
return "OK"
45+
if __name__ == "__main__":
46+
print(dispatch.run(fanout()))

examples/fanout/__init__.py

Whitespace-only changes.

examples/fanout/test_fanout.py

-19
This file was deleted.

examples/getting_started.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import requests
2+
3+
import dispatch
4+
5+
6+
@dispatch.function
7+
def publish(url, payload):
8+
r = requests.post(url, data=payload)
9+
r.raise_for_status()
10+
return r.text
11+
12+
13+
@dispatch.function
14+
async def getting_started():
15+
return await publish("https://httpstat.us/200", {"hello": "world"})
16+
17+
18+
if __name__ == "__main__":
19+
print(dispatch.run(getting_started()))

examples/getting_started/__init__.py

Whitespace-only changes.

examples/getting_started/app.py

-85
This file was deleted.

examples/getting_started/test_app.py

-40
This file was deleted.

0 commit comments

Comments
 (0)