Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-2.1· AnthropicHugging Face Arena Human Preference 55K

I have a list of operations which I can poll to check if they're completed. However the…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4-1106-preview’s answer.
  1. User

    I have a list of operations which I can poll to check if they're completed. However the polling is rate limited. What's the best way to handle this in Python?

  2. Claude Assistant

    Here are a few options to handle rate limited polling in Python:

    1. Use time.sleep() to add a delay between requests:
    import time
    
    while True:
      response = poll_api()
      if response.is_completed:
        break  
      time.sleep(1) # add 1 second delay
    
    1. Use a timer instead of sleep - this allows you to do other work in between polls:
    import threading
    
    def poll():
      response = poll_api()
      if not response.is_completed:
        timer = threading.Timer(1, poll)
        timer.start()
      else:
        print("Operation completed!")
    
    poll()
    
    1. Use asyncio to poll asynchronously:
    import asyncio
    
    async def poll():
      while True:
        response = await poll_api()
        if response.is_completed:
          return
        await asyncio.sleep(1)
    
    asyncio.run(poll())
    
    1. Use exponential backoff to increase the delays between requests after failures:
    import time, math
    
    delay = 1 
    max_delay = 16
    while True:
      response = poll_api()
      if response.is_completed:
        break
      
      time.sleep(delay)
      delay = min(delay * 2, max_delay)  
    

    The best approach depends on your specific application and needs. But all handle rate limiting in a robust way.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.