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
import asyncio
import async_timeout
import redis.asyncio as redis
from typing import AsyncGenerator
import strawberry
from selfprivacy_api.graphql import IsAuthenticated
@ -90,9 +93,22 @@ class Subscription:
@strawberry.subscription(permission_classes=[IsAuthenticated])
async def count(self, target: int = 100) -> AsyncGenerator[int, None]:
for i in range(target):
yield i
await asyncio.sleep(0.5)
r = redis.from_url('unix:///run/redis-sp-api/redis.sock')
pubsub = r.pubsub()
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)

View File

@ -16,6 +16,7 @@ let
typing-extensions
psutil
black
redis
fastapi
uvicorn
(buildPythonPackage rec {
@ -33,6 +34,7 @@ let
pydantic
pygments
poetry
redis
# flask-cors
(buildPythonPackage rec {
pname = "graphql-core";