0

I have a multithreading function that looks up and locks each item in a list. (I'm locking the query for each individual item so that other users can't hit it at the same time).

I also want to limit the rate in which users can look up a specific item, i.e. item1 was just searched so I don't want anyone else to be able to query item1 for 3 minutes.

def worker():
    """ Item search Multithreading Function """
    while True:
        item = q.get()

        try:
            with transaction.atomic():
                """ Lock the API for specified item search """
                locked = Models.objects.filter(name__icontains=item)[0]
                locked_search = Models.objects.select_for_update().get(pk=locked.pk)
                print("Search for " + str(locked_search) + ", " + str(item) + " is locked")
                
                ### ADD SERVER RATE LIMIT FOR ITEM SEARCHED HERE ###

                # ... Do work here

I've looked into Django Throttling and django-ratelimit but can't seem to find anything that limits the query rate of particular object. Anyone have any experience in this?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Anna M
  • 65
  • 6
  • Try to use Django's built-in caching framework to cache the result of a search for a specific item for a certain amount of time. – Sunderam Dubey Mar 04 '23 at 12:29

0 Answers0