Skip to content

Commit d465bc7

Browse files
committed
feat: add aiohttp test and exmaple
1 parent dcac2bb commit d465bc7

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ or not. Only event loops from asyncio can be patched;
4242
Loops from other projects, such as uvloop or quamash,
4343
generally can't be patched.
4444

45+
## Examples
46+
### [aiohttp](https://github.com/aio-libs/aiohttp)
47+
```py
48+
# /// script
49+
# requires-python = ">=3.5"
50+
# dependencies = [
51+
# "aiohttp",
52+
# "nest-asyncio2",
53+
# ]
54+
# ///
55+
import asyncio
56+
import nest_asyncio2
57+
import aiohttp
58+
59+
nest_asyncio2.apply()
60+
61+
async def f_async():
62+
# Note that ClientSession must be created and used
63+
# in the same event loop (under the same asyncio.run())
64+
async with aiohttp.ClientSession() as session:
65+
async with session.get('http://httpbin.org/get') as resp:
66+
print(resp.status)
67+
print(await resp.text())
68+
assert resp.status == 200
69+
70+
# async to sync
71+
def f():
72+
asyncio.run(f_async())
73+
74+
async def main():
75+
f()
76+
asyncio.run(main())
77+
```
78+
4579
## Comparison with `nest_asyncio`
4680
`nest-asyncio2` is a fork of the unmaintained [`nest_asyncio`](https://github.com/erdewit/nest_asyncio), with the following changes:
4781
- Python 3.12 support

nest_asyncio2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def run(main, *, debug=False, loop_factory=None):
7474
else:
7575
loop = loop_factory()
7676
new_event_loop = True
77+
_patch_loop(loop)
7778

7879
loop.set_debug(debug)
7980
task = asyncio.ensure_future(main, loop=loop)

tests/312_aiohttp.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# /// script
2+
# requires-python = ">=3.5"
3+
# dependencies = [
4+
# "aiohttp",
5+
# "nest-asyncio2",
6+
# ]
7+
#
8+
# [tool.uv.sources]
9+
# nest-asyncio2 = { path = "../", editable = true }
10+
# ///
11+
import warnings
12+
warnings.filterwarnings("default")
13+
14+
import asyncio
15+
import nest_asyncio2
16+
import aiohttp
17+
18+
with warnings.catch_warnings(record=True) as w:
19+
nest_asyncio2.apply()
20+
assert len(w) == 0, w
21+
22+
async def f_async():
23+
async with aiohttp.ClientSession() as session:
24+
async with session.get('http://httpbin.org/get') as resp:
25+
print(resp.status)
26+
print(await resp.text())
27+
assert resp.status == 200
28+
29+
def f():
30+
asyncio.run(f_async())
31+
32+
async def main():
33+
f()
34+
35+
with warnings.catch_warnings(record=True) as w:
36+
asyncio.run(main())
37+
assert len(w) == 0, w

tests/test.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ if (!$?) {
6060
Test -V @("3.12", "3.13", "3.14") -Py 314_task.py
6161
Test -V @("3.12", "3.13", "3.14") -Py 314_task_mix.py
6262

63+
Test -V @("3.11", "3.12", "3.13", "3.14") -Py 312_aiohttp.py
64+
6365
popd

0 commit comments

Comments
 (0)