![]() Local time_left = redis.call('PTTL', key) Wrote this function, using redis to count requests (based on this tutorial) import redisĭef limit_request(request_to_make, limit=3, per=1, request_name='test', **kwargs): Would be glad, if someone suggests any ideas or even examples of code (external api does not provide any x-rate-limit header) I thought of making rate limit with redis, so every time in every thread and process bot will check the time of last 3 requests, but still i am not sure, that this is the right way, and i am not sure, how to write this. And, secondly, even if i set number of workers to default (1 worker), i still get 429 error, so i think, that my code won't work as i wanted at all. And, because i needed to be sure, that only one thread would do this kind of comparison simultaneously, i made lock_queue.īut, the problem is that, firstly, gunicorn uses 5 workers, so there will be always possibility, that all messages from users will be handled in different processes, and these processes will have their own queues. The logic was that, as i thought, because module pyTelegramBotAPI uses threads for faster updates handling, all threads would check requests_queue, which will have time of 3 last api_requests, and so the time of the first of 3 requests made will be compared to the current time (to check, if a second passed). Result = make_api_request(argument) # requests are made too by external module. Passed_time = current_time - first_request_timeįirst_request_time = vk_requests_queue.get() ('First request time: ' + str(first_request_time)) First i thought of doing it with Queue with code like this: lock_queue = Queue(1)įirst_request_time = requests_queue.get() I need to configure a bot with such rate limit. The problem is, that this external api has limit for 3 requests per second. In my bot user can call a command, which will use an external api to get some information. In app.py i imported this function, so, whenever an update comes, app.py will call this function, and bot will handle the update. In bot.py i have a function for processing updates: def process_the_update(update): Gunicorn is working with 5 workers for better speed, structure of my project looks like that: app.py I am developing a bot with module pyTelegramBotAPI, which is working via webhooks with installed Flask+Gunicorn as server for webhooks.
0 Comments
Leave a Reply. |