Skip to content
Advertisement

Asynchronous code failure when connecting to redis

I created a small class to perform basic operations with redis, using aioredis.

JavaScript

And a test handler to call the write/read operation for redis

JavaScript

But get error

JavaScript

I guess the problem could be that the asynchronous code has to be run through an event loop

JavaScript

But I can’t understand how I can build this logic into my code

Advertisement

Answer

Your problem is how you use create_connection. You have to call it and await what it returns.

JavaScript

You’ll then need to await set and get as well. As a one-liner this would get messy.

JavaScript

To help clean this up, you should split the awaits into separate statements.

JavaScript

Creating a new connection each time you need to perform an operation can be expensive. I’d recommend changing create_connection in one or two ways.

Either have it attach the connection to your instance

JavaScript

You can call this after you instantiate an instance of RedisService and then use

JavaScript

Or you can switch to using a connection pool.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement