Skip to content

Timeout for hung requests #39

Open

Description

@christian-oudard

Sometimes when downloading a lot of data from NasdaqDataLink, I get a hung connection during the ssl handshake. The requests library, which is being used to make requests, can take a timeout parameter, but this is not exposed through the API as far as I can tell.

What's the best way to retry after a timeout?

I noticed that the ApiConfig and Connection have retry parameters, but it still appears to be hanging.

Activity

  1. clientsuccessnasdaq commented on Sep 13, 2023

    @clientsuccessnasdaq
  2. clientsuccessnasdaq commented on Sep 14, 2023

    @clientsuccessnasdaq
  3. clientsuccessnasdaq commented on Sep 18, 2023

    @clientsuccessnasdaq
  4. christian-oudard commented on Dec 15, 2025

    @christian-oudard
    Author

    The error is that sometimes the request simply does not complete. This is normal and expected some percentage of the time for networked applications.

    I forked the library and added a timeout parameter. This allows me to do this is in my code:

    def ndl_get_table_with_retry(table, **kwargs):
        timeouts = [5, 10, 20, 40, 80]
    
        last_error = None
        for attempt, timeout in enumerate(timeouts):
            try:
                return ndl.get_table(table, timeout=timeout, **kwargs)
            except Exception as e:
                last_error = e
                error_msg = str(e).lower()
                # Don't retry on auth errors or invalid requests
                if 'unauthorized' in error_msg or 'api key' in error_msg:
                    raise
                if 'invalid' in error_msg and 'ticker' in error_msg:
                    raise
                # Log retry attempt
                if attempt < len(timeouts) - 1:
                    print(f"  Nasdaq API timeout ({timeout}s), retrying with {timeouts[attempt + 1]}s timeout...")
                time.sleep(1)  # Brief pause before retry
    
        # All retries exhausted
        raise TimeoutError(
            f"Nasdaq Data Link API failed after {len(timeouts)} attempts "
            f"(timeouts: {timeouts}). Last error: {last_error}"
        )
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions