mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-09-29 11:37:52 +00:00
Test of the redis subscription
This commit is contained in:
parent
0a09a338b8
commit
3ba94e02da
|
@ -2,6 +2,9 @@
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import async_timeout
|
||||||
|
import redis.asyncio as redis
|
||||||
|
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
import strawberry
|
import strawberry
|
||||||
from selfprivacy_api.graphql import IsAuthenticated
|
from selfprivacy_api.graphql import IsAuthenticated
|
||||||
|
@ -90,9 +93,22 @@ class Subscription:
|
||||||
|
|
||||||
@strawberry.subscription(permission_classes=[IsAuthenticated])
|
@strawberry.subscription(permission_classes=[IsAuthenticated])
|
||||||
async def count(self, target: int = 100) -> AsyncGenerator[int, None]:
|
async def count(self, target: int = 100) -> AsyncGenerator[int, None]:
|
||||||
for i in range(target):
|
r = redis.from_url('unix:///run/redis-sp-api/redis.sock')
|
||||||
yield i
|
pubsub = r.pubsub()
|
||||||
await asyncio.sleep(0.5)
|
await pubsub.psubscribe("__keyspace@0__:api_test")
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
async with async_timeout.timeout(1):
|
||||||
|
message = await pubsub.get_message()
|
||||||
|
if message:
|
||||||
|
if message['data'] == 'set':
|
||||||
|
await r.get('api_test')
|
||||||
|
yield int(await r.get('api_test'))
|
||||||
|
else:
|
||||||
|
await asyncio.sleep(0.01)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
schema = strawberry.Schema(query=Query, mutation=Mutation, subscription=Subscription)
|
schema = strawberry.Schema(query=Query, mutation=Mutation, subscription=Subscription)
|
||||||
|
|
|
@ -16,6 +16,7 @@ let
|
||||||
typing-extensions
|
typing-extensions
|
||||||
psutil
|
psutil
|
||||||
black
|
black
|
||||||
|
redis
|
||||||
fastapi
|
fastapi
|
||||||
uvicorn
|
uvicorn
|
||||||
(buildPythonPackage rec {
|
(buildPythonPackage rec {
|
||||||
|
@ -33,6 +34,7 @@ let
|
||||||
pydantic
|
pydantic
|
||||||
pygments
|
pygments
|
||||||
poetry
|
poetry
|
||||||
|
redis
|
||||||
# flask-cors
|
# flask-cors
|
||||||
(buildPythonPackage rec {
|
(buildPythonPackage rec {
|
||||||
pname = "graphql-core";
|
pname = "graphql-core";
|
||||||
|
|
Loading…
Reference in a new issue