Skip to content

Commit 0094dba

Browse files
committed
Add nats-client package
Signed-off-by: Casper Beyer <[email protected]>
1 parent d6f541a commit 0094dba

File tree

20 files changed

+4452
-1
lines changed

20 files changed

+4452
-1
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
os: ["ubuntu-latest", "macos-latest"]
6565
>>>>>>> 44fb982 (Add nats-server package)
6666
nats-server-version: ["latest"]
67-
project: ["nats-server"]
67+
project: ["nats-server", "nats-client"]
6868
steps:
6969
- name: Checkout repository
7070
<<<<<<< HEAD

nats-client/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# NATS Client
2+
3+
A Python client for the NATS messaging system.
4+
5+
## Features
6+
7+
- Support for publish/subscribe
8+
- Support for request/reply
9+
- Support for queue groups
10+
- Support for multi-value message headers
11+
12+
## Installation
13+
14+
```bash
15+
pip install nats-client
16+
```
17+
18+
## Usage
19+
20+
```python
21+
import asyncio
22+
from nats.client import connect
23+
24+
async def main():
25+
client = await connect("nats://localhost:4222")
26+
27+
# Subscribe
28+
async with await client.subscribe("foo") as subscription:
29+
# Publish
30+
await client.publish("foo", "Hello World!")
31+
32+
# Receive message
33+
message = await subscription.next()
34+
print(f"Received: {message.data}")
35+
36+
await client.close()
37+
38+
if __name__ == "__main__":
39+
asyncio.run(main())
40+
```
41+
42+
## 🚀 Performance
43+
44+
This client implementation delivers significant performance improvements over the nats.aio client, particularly for high-frequency, small message workloads.
45+
46+
Do note tho, it is not as feature complete at this point in time.
47+
48+
| Message Size | nats.py (python3) | nats.py (pypy3) | experimental-nats.py (python3) | experimental-nats (pypy3) | Performance Gain |
49+
|--------------|-------------------|-----------------|--------------------------------|---------------------------|------------------|
50+
| 1B | 127,411 | 153,009 | 1,522,673 | **5,376,113** | **35.1x** 🚀 |
51+
| 2B | 136,485 | 148,981 | 1,544,513 | **5,396,347** | **36.2x** 🚀 |
52+
| 4B | 131,630 | 149,297 | 1,548,191 | **5,356,600** | **35.9x** 🚀 |
53+
| 8B | 138,229 | 141,117 | 1,530,825 | **5,307,400** | **37.6x** 🚀 |
54+
| 16B | 140,874 | 149,826 | 1,539,244 | **5,211,168** | **34.8x** 🚀 |
55+
| 32B | 141,427 | 146,670 | 1,515,068 | **5,115,238** | **34.9x** 🚀 |
56+
| 64B | 145,257 | 153,542 | 1,505,724 | **5,339,967** | **34.8x** 🚀 |
57+
| 128B | 163,181 | 164,723 | 1,479,100 | **4,923,321** | **29.9x** 🔥 |
58+
| 256B | 145,824 | 161,017 | 1,452,996 | **4,130,165** | **25.7x** 🔥 |
59+
| 512B | 243,641 | 277,321 | 1,297,250 | **3,430,092** | **12.4x**|
60+
| 1K | 738,895 | 802,283 | 1,253,102 | **2,374,747** | **3.0x**|
61+
| 2K | 696,945 | 736,925 | 1,060,123 | **1,381,177** | **1.9x**|
62+
| 4K | 577,335 | 625,935 | 798,797 | **814,393** | **1.3x**|
63+
| 8K | 414,077 | 463,383 | 532,429 | 450,211 | 0.97x |
64+
| 16K | 266,104 | 309,680 | 345,651 | 228,815 | 0.74x |
65+
| 32K | 102,460 | 128,852 | 166,028 | 125,662 | 0.98x |
66+
| 64K | 55,208 | 63,563 | 74,359 | 56,804 | 0.89x |
67+
68+
### Key Performance Insights
69+
70+
**🎯 Sweet Spot: Small to Medium Messages**
71+
- **35-37x faster** for tiny messages (1B-64B)
72+
- **25-30x faster** for small messages (128B-256B)
73+
- **12x faster** for medium messages (512B)
74+
75+
### Benchmark Environment
76+
77+
- **CPU**: Apple M3 Max
78+
- **Memory**: 36 GB
79+
- **Python**: 3.x
80+
- **PyPy**: 3.x
81+
82+
> **Note**: Benchmarks may vary based on your specific hardware, network conditions, and NATS server configuration. We recommend running your own benchmarks for production workloads.

nats-client/pyproject.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "nats-client"
7+
version = "0.0.0"
8+
description = "NATS client implementation in Python"
9+
authors = [{ name = "Casper Beyer", email = "[email protected]" }]
10+
dependencies = []
11+
requires-python = ">=3.10"
12+
readme = "README.md"
13+
license = { text = "MIT" }
14+
15+
[project.urls]
16+
Documentation = "https://github.com/nats-io/nats.py"
17+
Issues = "https://github.com/nats-io/nats.py/issues"
18+
Source = "https://github.com/nats-io/nats.py"
19+
20+
[tool.hatch.metadata]
21+
allow-direct-references = true
22+
23+
[tool.hatch.build.targets.sdist]
24+
include = ["src/nats"]
25+
26+
[tool.hatch.build.targets.wheel]
27+
packages = ["src/nats"]
28+
namespace-packages = ["nats"]
29+
30+
[tool.hatch.envs.hatch-test]
31+
extra-dependencies = [
32+
"nats-server @ file:../nats-server",
33+
"pytest-asyncio",
34+
"pytest-benchmark",
35+
"pytest-xdist",
36+
"coverage",
37+
"pytest-cov",
38+
]
39+
40+
[tool.pytest.ini_options]
41+
asyncio_mode = "auto"
42+
asyncio_default_fixture_loop_scope = "function"

0 commit comments

Comments
 (0)