Compare commits

...

2 Commits

Author SHA1 Message Date
Inex Code 33bd7f84b9 Fix message type checking 2022-10-24 04:20:29 +03:00
Inex Code 3ba94e02da Test of the redis subscription 2022-10-24 03:55:48 +03:00
2 changed files with 21 additions and 3 deletions

View File

@ -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'] == b'set':
retrieved_value = await r.get('api_test')
yield int(retrieved_value)
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)

View File

@ -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";