How to set a timeout in Python

Use the timeout_decorator library.

pip install timeout_decorator

then use as:

from timeout_decorator import timeout

@timeout(30)
def process_company(company):
    try:
        xxx
    except Exception as e:
        print(f"❌ {e}")

for count_company, company in enumerate(all_companies_with_no_url):
    try:
        process_company(company)
    except TimeoutError:
        print("❌ Timeout error occurred. Processing took too long.")
    except Exception as e:
        print(f"❌ {e}")

links

social