Skip to content

Commit 7672974

Browse files
authored
Merge pull request #13 from standy66/anyio
[WIP] feat: migrate to anyio
2 parents e43d890 + 8b1ec1a commit 7672974

47 files changed

Lines changed: 678 additions & 410 deletions

Some content is hidden

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

.travis.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ stages:
99
- name: deploy
1010
if: tag IS present AND type != pull_request
1111
env:
12-
- PYTHON_IMAGE="python:3.6"
13-
- PYTHON_IMAGE="python:3.7"
14-
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0"
12+
- PYTHON_IMAGE="python:3.6" PURERPC_BACKEND="asyncio"
13+
- PYTHON_IMAGE="python:3.6" PURERPC_BACKEND="curio"
14+
- PYTHON_IMAGE="python:3.6" PURERPC_BACKEND="trio"
15+
- PYTHON_IMAGE="python:3.7" PURERPC_BACKEND="asyncio"
16+
- PYTHON_IMAGE="python:3.7" PURERPC_BACKEND="curio"
17+
- PYTHON_IMAGE="python:3.7" PURERPC_BACKEND="trio"
18+
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0" PURERPC_BACKEND="asyncio"
19+
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0" PURERPC_BACKEND="curio"
20+
- PYTHON_IMAGE="standy/pypy:3.6-6.1.0" PURERPC_BACKEND="trio"
1521

1622
script:
17-
- ./ci/run_tests_in_docker.sh $PYTHON_IMAGE
23+
- ./ci/run_tests_in_docker.sh $PYTHON_IMAGE $PURERPC_BACKEND
1824

1925
jobs:
2026
include:

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ FROM ${BASE_IMAGE}
33
COPY . /purerpc
44
WORKDIR /purerpc
55
RUN pip install .
6+
RUN pip install curio trio # Optional, for tests

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
[![Build Status](https://travis-ci.org/standy66/purerpc.png?branch=master)](https://travis-ci.org/standy66/purerpc)
44

5-
Asynchronous pure Python gRPC server and client implementation using
6-
[curio](https://github.com/dabeaz/curio) and [hyper-h2](https://github.com/python-hyper/hyper-h2)
5+
Asynchronous pure Python gRPC server and client implementation supporting
6+
[asyncio](https://docs.python.org/3/library/asyncio.html),
7+
[curio](https://github.com/dabeaz/curio) and
8+
[trio](https://github.com/python-trio/trio).
79

810
## Requirements
911

@@ -24,6 +26,8 @@ Latest development version:
2426
pip install git+https://github.com/standy66/purerpc.git
2527
```
2628

29+
By default purerpc uses asyncio event loop, if you want to use curio or trio, please install them manually.
30+
2731
## protoc plugin
2832

2933
purerpc adds `protoc-gen-purerpc` plugin for `protoc` to your `PATH` enviroment variable
@@ -62,16 +66,16 @@ class Greeter(GreeterServicer):
6266

6367
server = Server(50055)
6468
server.add_service(Greeter().service)
65-
server.serve()
69+
server.serve(backend="asyncio")
6670
```
6771

6872
### Client
6973

7074
```python
7175
import curio
76+
import purerpc
7277
from greeter_pb2 import HelloRequest, HelloReply
7378
from greeter_grpc import GreeterStub
74-
from purerpc import Channel
7579

7680

7781
async def gen():
@@ -80,22 +84,19 @@ async def gen():
8084

8185

8286
async def main():
83-
channel = Channel("localhost", 50055)
84-
# This is optional, will be run automatically on the first request
85-
await channel.connect()
86-
stub = GreeterStub(channel)
87-
88-
reply = await stub.SayHello(HelloRequest(name="World"))
89-
print(reply.message)
90-
91-
async for reply in stub.SayHelloToMany(gen()):
87+
async with purerpc.insecure_channel("localhost", 50055) as channel:
88+
stub = GreeterStub(channel)
89+
reply = await stub.SayHello(HelloRequest(name="World"))
9290
print(reply.message)
9391

92+
async for reply in stub.SayHelloToMany(gen()):
93+
print(reply.message)
94+
9495

9596
if __name__ == "__main__":
96-
curio.run(main)
97+
curio.run(main) # Or trio.run(main)
9798
```
9899

99-
You can mix server and client code, for example make a server that requests something using purerpc from another server, etc.
100+
You can mix server and client code, for example make a server that requests something using purerpc from another gRPC server, etc.
100101

101102
More examples in `misc/` folder

ci/run_tests_in_docker.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
set -e
44

55
BASE_IMAGE="$1"
6+
PURERPC_BACKEND="$2"
67
BUILD_TAG=${BASE_IMAGE//:/-}
78
BUILD_TAG=${BUILD_TAG//\//-}
89

910
docker build --build-arg BASE_IMAGE=${BASE_IMAGE} -t "standy/purerpc:${BUILD_TAG}" .
10-
docker run -it "standy/purerpc:$BUILD_TAG" bash -c 'python setup.py test'
11+
echo "Runnig tests with $PURERPC_BACKEND backend"
12+
docker run -it -e PURERPC_BACKEND=${PURERPC_BACKEND} "standy/purerpc:$BUILD_TAG" bash -c 'python setup.py test'

commitlint.config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
rules: {
3+
'body-leading-blank': [1, 'always'],
4+
'footer-leading-blank': [1, 'always'],
5+
'header-max-length': [2, 'always', 72],
6+
'scope-case': [2, 'always', 'lower-case'],
7+
'subject-case': [
8+
2,
9+
'never',
10+
['sentence-case', 'start-case', 'pascal-case', 'upper-case']
11+
],
12+
'subject-empty': [2, 'never'],
13+
'subject-full-stop': [2, 'never', '.'],
14+
'type-case': [2, 'always', 'lower-case'],
15+
'type-empty': [2, 'never'],
16+
'type-enum': [
17+
2,
18+
'always',
19+
[
20+
'build',
21+
'chore',
22+
'ci',
23+
'docs',
24+
'feat',
25+
'fix',
26+
'perf',
27+
'refactor',
28+
'revert',
29+
'style',
30+
'test'
31+
]
32+
]
33+
}
34+
};
File renamed without changes.

misc/greeter/baseline_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import grpc
77

8-
from greeter_pb2 import HelloRequest, HelloReply
9-
from greeter_pb2_grpc import GreeterServicer, add_GreeterServicer_to_server
8+
from generated.greeter_pb2 import HelloReply
9+
from generated.greeter_pb2_grpc import GreeterServicer, add_GreeterServicer_to_server
1010

1111
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
1212

misc/greeter/client.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import curio
22
import time
3-
from purerpc.client import Channel, Client
4-
from greeter_pb2 import HelloRequest, HelloReply
5-
from greeter_grpc import GreeterStub
6-
from purerpc.utils import print_memory_growth_statistics
3+
import purerpc
4+
from generated.greeter_pb2 import HelloRequest
5+
from generated.greeter_grpc import GreeterStub
76

87

98
async def worker(channel):
@@ -16,14 +15,13 @@ async def worker(channel):
1615

1716
async def main_coro():
1817
# await curio.spawn(print_memory_growth_statistics(), daemon=True)
19-
channel = Channel("localhost", 50055)
20-
await channel.connect()
21-
for i in range(100):
22-
start = time.time()
23-
async with curio.TaskGroup() as task_group:
24-
for i in range(100):
25-
await task_group.spawn(worker(channel))
26-
print("RPS: {}".format(10000 / (time.time() - start)))
18+
async with purerpc.insecure_channel("localhost", 50055) as channel:
19+
for i in range(100):
20+
start = time.time()
21+
async with curio.TaskGroup() as task_group:
22+
for i in range(100):
23+
await task_group.spawn(worker(channel))
24+
print("RPS: {}".format(10000 / (time.time() - start)))
2725

2826

2927
def main():

misc/greeter/client_grpcio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import grpc
2-
from greeter_pb2 import HelloRequest, HelloReply
3-
from greeter_pb2_grpc import GreeterStub
2+
from generated.greeter_pb2 import HelloRequest
3+
from generated.greeter_pb2_grpc import GreeterStub
44

55

66
def main():

misc/greeter/generated/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)