how to make the next task only execute after 3 secs from last execution?

I would like to make the @shared_task only run after 3 secs from the last execution.

The reason is my django app need to call another API that have rate limit 1 request 1 sec.

My code:

views.py

@router.post(
    "/crawl",
    response=StdResponse[Union[Data, None]],
)
def crawl_me(
    request,
    payload: DataCrawl,
):

    ...

    task_sm.crawl_book.apply_async((book_id, book_name), countdown=10)
    
    ...

tasks.py

@shared_task
def crawl_book(id, name):
   book.get_me(...)

@shared_task(rate_limit="1/h")
def request_api(api_provider, extra_info=None, *args, **kwargs):
    r = requests.get(*args, **kwargs)

book.py

def get_me(xx):
   response = task_sm.request_api(self.api_provider, url=url, headers=self.headers, params=querystring)

the issue is, it not wait for next hour to execute next API request. what should I change?

Leave a Comment