Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

pubsub.get_message(blocking_timeout) don't work with async_timeout.timeout #1424

Open
1 task done
heyfavour opened this issue Sep 27, 2022 · 2 comments
Open
1 task done
Labels

Comments

@heyfavour
Copy link

heyfavour commented Sep 27, 2022

Describe the bug

await pubsub.subscribe(self.channel)
try:
    start_time = datetime.datetime.now()
    async with async_timeout.timeout(blocking_timeout):
        while True:
            message = await pubsub.get_message(ignore_subscribe_messages=True, timeout=blocking_timeout)
            end_time = datetime.datetime.now()
            logger.info(f"{str(message)},{blocking_timeout}==={end_time-start_time}")
            if message is not None: break
except asyncio.TimeoutError:
    pass

i set get_message's blocking_timeout equals async_timeout 's blocking_timeout
but i found when they are similar or equal , async_timeout.timeout will not works it will always run util get a message

To Reproduce

python=3.10.2
aioredis=2.10.1
async-timeout = 4.0.2
mycode

Expected behavior

catch timeouterror

Logs/tracebacks

2022-09-27 14:42:21,621 - 269 - [0]cust_no:product_no0:89e4caf93e2f11ed94e1484d7ecf8cb7
2022-09-27 14:42:21,622 - 269 - [1]cust_no:product_no1:89e4f2063e2f11edab66484d7ecf8cb7
2022-09-27 14:42:21,623 - 269 - [2]cust_no:product_no0:89e5191b3e2f11edba6c484d7ecf8cb7
2022-09-27 14:42:21,647 - 273 - [0] cust_no:product_no0 True {'89e4caf93e2f11ed94e1484d7ecf8cb7': '1'} 第一次获取锁
2022-09-27 14:42:21,651 - 273 - [1] cust_no:product_no1 True {'89e4f2063e2f11edab66484d7ecf8cb7': '1'} 第一次获取锁
2022-09-27 14:42:21,653 - 185 - None,3===0:00:00.007000
2022-09-27 14:42:21,662 - 258 - [0] cust_no:product_no0 True {'89e4caf93e2f11ed94e1484d7ecf8cb7': '2'} 第二次获取锁
2022-09-27 14:42:21,667 - 258 - [1] cust_no:product_no1 True {'89e4f2063e2f11edab66484d7ecf8cb7': '2'} 第二次获取锁
2022-09-27 14:42:24,657 - 185 - None,3===0:00:03.012440
2022-09-27 14:42:24,664 - 260 - [0] sleep ---> key:{'89e4caf93e2f11ed94e1484d7ecf8cb7': '2'} 第二次锁后sleep
2022-09-27 14:42:24,664 - 260 - [1] sleep ---> key:{'89e4f2063e2f11edab66484d7ecf8cb7': '2'} 第二次锁后sleep
2022-09-27 14:42:24,677 - 262 - [0] cust_no:product_no0_release2:2 {'89e4caf93e2f11ed94e1484d7ecf8cb7': '1'} 释放第二次锁
2022-09-27 14:42:24,678 - 262 - [1] cust_no:product_no1_release2:2 {'89e4f2063e2f11edab66484d7ecf8cb7': '1'} 释放第二次锁
2022-09-27 14:42:27,675 - 185 - None,3===0:00:06.030194
2022-09-27 14:42:27,682 - 276 - [0] keycust_no:product_no0 ttl:26990 查看剩余时间
2022-09-27 14:42:27,683 - 276 - [1] keycust_no:product_no1 ttl:26989 查看剩余时间
2022-09-27 14:42:27,695 - 278 - [0] keycust_no:product_no0 ttl:36977 续命
2022-09-27 14:42:27,696 - 278 - [1] keycust_no:product_no1 ttl:36976 续命
2022-09-27 14:42:27,702 - 185 - {'type': 'message', 'pattern': None, 'channel': 'redisson_lock_channel:cust_no:product_no0', 'data': '89e4caf93e2f11ed94e1484d7ecf8cb7'},3===0:00:06.057626
2022-09-27 14:42:29,720 - 281 - [0] cust_no:product_no0_release1:1 {} 释放第一次锁
2022-09-27 14:42:29,728 - 281 - [1] cust_no:product_no1_release1:1 {} 释放第一次锁
2022-09-27 14:42:29,740 - 273 - [2] cust_no:product_no0 True {'89e5191b3e2f11edba6c484d7ecf8cb7': '1'} 第一次获取锁
2022-09-27 14:42:29,757 - 258 - [2] cust_no:product_no0 True {'89e5191b3e2f11edba6c484d7ecf8cb7': '2'} 第二次获取锁
2022-09-27 14:42:32,775 - 260 - [2] sleep ---> key:{'89e5191b3e2f11edba6c484d7ecf8cb7': '2'} 第二次锁后sleep
2022-09-27 14:42:32,789 - 262 - [2] cust_no:product_no0_release2:2 {'89e5191b3e2f11edba6c484d7ecf8cb7': '1'} 释放第二次锁

Python Version

3.10.2

aioredis Version

4.0.2

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@heyfavour heyfavour added the bug label Sep 27, 2022
@heyfavour
Copy link
Author

heyfavour commented Sep 27, 2022

import asyncio
import datetime
import aioredis
import async_timeout


async def run():
    try:
        key = "cust_no:product_no0"
        channel = f"redisson_lock_channel:{key}"
        redis = aioredis.from_url("redis://49.235.242.224:6379", password="wzx940516", decode_responses=True, )
        pubsub = redis.pubsub()
        start_time = datetime.datetime.now()
        await pubsub.subscribe(channel)
        try:
            start_time = datetime.datetime.now()
            async with async_timeout.timeout(3):
                while True:
                    message = await pubsub.get_message(ignore_subscribe_messages=True, timeout=1)
                    end_time = datetime.datetime.now()
                    print(f"{str(message)},{end_time - start_time}")
                    if message is not None: break
        except asyncio.TimeoutError:
            pass
    except asyncio.TimeoutError:
        end_time = datetime.datetime.now()
        print("outer error",end_time-start_time)

if __name__ == '__main__':
    asyncio.run(run())

i code a easy demo ,if you try enough times, i will not raise TimeoutError

@heyfavour
Copy link
Author

i code my aioredis lock,i think aioredis lock use threading lock is not suitable.
code url:
https://github.com/heyfavour/code_sniippet/tree/master/aioredislock

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant