https://github.com/newswangerd/django_grpc_querysets
Django gRPC client and server that mimics the queryset API
https://github.com/newswangerd/django_grpc_querysets
Last synced: 2 months ago
JSON representation
Django gRPC client and server that mimics the queryset API
- Host: GitHub
- URL: https://github.com/newswangerd/django_grpc_querysets
- Owner: newswangerd
- Created: 2024-04-21T21:08:17.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-21T21:08:49.000Z (about 1 year ago)
- Last Synced: 2025-01-18T00:23:04.146Z (4 months ago)
- Language: Python
- Size: 31.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gRPC Querysets
This library provides a gRPC client and server that allows for Django querysets to be executred remotetly.
## Demo
### Server
Create a management command for running the gRPC process:```python
from concurrent import futuresimport grpc
from django.core.management.base import BaseCommand
from grpc_querysets.server import QueryServer
from grpc_querysets.lib import query_pb2_grpcfrom test_app import serializers
class Command(BaseCommand):
def handle(self, *args, **options):
port = "50051"
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))# Registers type "test" with TestModelSerializer
query_pb2_grpc.add_RemoteQuerysetServicer_to_server(
QueryServer({"test": serializers.TestModelSerializer}), server
)
server.add_insecure_port("[::]:" + port)
server.start()
print("Server started, listening on " + port)
server.wait_for_termination()
```Launch the gRPC server with `$ python manage.py start_grpc_server`
### Client
With the server set up, we can now interract with our Django app using an API client that mimics the Django queryset API.
```
$ python manage.py shell>>> from test_app import serializers
>>> from grpc_querysets.grpc_queryset import RemoteQueryset
>>> qs = RemoteQueryset(serializers.TestModelSerializer, "test")>>> qs.create(name="foo", f1="other field")
sending request
request finished in 0.008120059967041016 seconds
{'name': 'foo', 'f1': 'other field', 'f2': None, 'my_fk': None}>>> qs.count()
sending request
request finished in 0.005173921585083008 seconds
1>>> qs.create(name="foo2")
sending request
request finished in 0.008412837982177734 seconds
{'name': 'foo2', 'f1': None, 'f2': None, 'my_fk': None}>>> qs.create(name="my_name")
sending request
request finished in 0.006021261215209961 seconds
{'name': 'my_name', 'f1': None, 'f2': None, 'my_fk': None}>>> list(qs.all())
sending request
request finished in 0.006189823150634766 seconds
request finished in 0.0001957416534423828 seconds
request finished in 0.00018596649169921875 seconds
[{'name': 'foo', 'f1': 'other field', 'f2': None, 'my_fk': None}, {'name': 'foo2', 'f1': None, 'f2': None, 'my_fk': None}, {'name': 'my_name', 'f1': None, 'f2': None, 'my_fk': None}]>>> list(qs.exclude(name__icontains="foo"))
sending request
request finished in 0.00799417495727539 seconds
[{'name': 'my_name', 'f1': None, 'f2': None, 'my_fk': None}]>>> list(qs.filter(name__icontains="foo"))
sending request
request finished in 0.005292177200317383 seconds
request finished in 0.00015282630920410156 seconds
[{'name': 'foo', 'f1': 'other field', 'f2': None, 'my_fk': None}, {'name': 'foo2', 'f1': None, 'f2': None, 'my_fk': None}]>>> qs.filter(name__icontains="foo").count()
sending request
request finished in 0.006677865982055664 seconds
2>>> qs.all().delete()
sending request
request finished in 0.009694814682006836 seconds>>> qs.count()
sending request
request finished in 0.004435062408447266 seconds
0
```