-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathasync.py
More file actions
46 lines (36 loc) · 1.56 KB
/
Copy pathasync.py
File metadata and controls
46 lines (36 loc) · 1.56 KB
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
from google_custom_search import CustomSearch, AiohttpAdapter, HttpxAdapter
import asyncio
from pprint import pprint
async def main_aiohttp():
"""Example using AiohttpAdapter with async iterator."""
async with AiohttpAdapter() as adapter:
# Using asearch returns an async generator, so we need async for
async for item in CustomSearch(adapter).asearch(
"python frameworks httpx vs aiohttp - when to use aiohttp", limit=5
):
pprint(item.data)
print("-" * 80)
async def main_httpx():
"""Example using HttpxAdapter with async iterator."""
async with HttpxAdapter() as adapter:
# Using asearch returns an async generator, so we need async for
async for item in CustomSearch(adapter).asearch(
"python frameworks httpx vs aiohttp - when to use httpx", limit=5
):
pprint(item.data)
print("-" * 80)
async def main_httpx_list():
"""Example using HttpxAdapter with search_async to get a list."""
async with HttpxAdapter() as adapter:
# Using search_async returns a list, so we can use regular for
results = await adapter.search_async("python async web frameworks")
for item in results[:5]:
pprint(item.data)
print("-" * 80)
# Run examples
print("=== Running AiohttpAdapter example ===")
asyncio.run(main_aiohttp())
print("\n=== Running HttpxAdapter with async iterator example ===")
asyncio.run(main_httpx())
print("\n=== Running HttpxAdapter with search_async example ===")
asyncio.run(main_httpx_list())