0

I'm new to manipulate connection with Redis server in python. I'm wondering that is there any way to figure out the situation below:

What I'm doing is

There are three mainly step for my program:
1. subscribe channel on Redis
2. Get the message from Redis and convert to custom model(or dict)
3. Sent the custom model to my server using socketio

What I write is

async def subscribeRedisChannel():
    for item in r.subscriber.listen():
        logger.info("[DEBUG]Checkin Result from Redis: {}".format(item))
        if item['type'] == 'message':
            checkInSubject.on_next(item)
            await sio.emit('punchByEquipment', data = keep.value)

Environment

python3
socketio
asyncio
rx
redis

The problem I faced

if I call the subscribeRedisChannel function in my main function, the program will be hang on it. Other program cannot work correctly.

1 Answers1

0

For those people who have the same or similar issues.

In this problem, I realize that the mainly issues is using blocking message. At he begin I follow the OLD-VERSION example code to implement the connection mechanism between redis and my program

 for message in p.listen():
     # do something with the message

However, you can directly call get_message() to listen the channel in non-blocking mechanism. For the usage example, you can find it in the test code

 while True:
     message = p.get_message()
     if message:
         # do something with the message
     time.sleep(0.001)  # be nice to the system :)

If you are looking for the specific explanation, check this answer in other thread.