Answers for "python timeout error"

2

python timeout

You may use the signal package if you are running on UNIX:

import signal

# Register an handler for the timeout
def handler(signum, frame):
  print("Forever is over!")
       raise Exception("end of time")
   

# This function *may* run for an indetermined time...
def loop_forever():
     import time
     while 1:
         print("sec")
         time.sleep(1)
         
         

# Register the signal function handler
signal.signal(signal.SIGALRM, handler)


# Define a timeout for your function
signal.alarm(10)
0

try:
    loop_forever()
except Exception, exc:
    print(exc)
Posted by: Guest on February-26-2021
0

python timeout exception

--------------------------------------------------------------
Timeout function
--------------------------------------------------------------
import asyncio
from async_timeout import timeout

class Student:
  	def __init__(self):
      	self.queue = asyncio.Queue()
      	pass
  	
	async def function(self):
      	try:
    		async with timeout(300): # 5 minutes...
              	#source = await self.queue.get()
            	
                #do what u need to do
                pass
                
        except asyncio.TimeoutError as e:
        	print(e)
            
            
--------------------------------------------------------------
Requests
--------------------------------------------------------------
import requests as r

class Student:
	def __init__(self):
      	pass
      
  	def function(url:string):
		try:
    		data = r.get(url, timeout=10.0)
		except requests.Timeout as err:
    		logger.error({"message": err.message})
		except Exception as err:
          	print(err)
        return data # or what ever u need to return
Posted by: Guest on February-19-2021
0

index error in python

# main.py
import datetime

from gw_utility.book import Book
from gw_utility.logging import Logging


def main():
    try:
        # Create list and populate with Books.
        books = list()
        books.append(Book("Shadow of a Dark Queen", "Raymond E. Feist", 497, datetime.date(1994, 1, 1)))
        books.append(Book("Rise of a Merchant Prince", "Raymond E. Feist", 479, datetime.date(1995, 5, 1)))
        books.append(Book("Rage of a Demon King", "Raymond E. Feist", 436, datetime.date(1997, 4, 1)))

        # Output Books in list, with and without index.
        Logging.line_separator('Books')
        log_list(books)
        Logging.line_separator('Books w/ index')
        log_list(books, True)
        # Output list element outside bounds.
        Logging.line_separator('books[len(books)]')
        Logging.log(f'books[{len(books)}]: {books[len(books)]}')
    except IndexError as error:
        # Output expected IndexErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)


def log_list(collection, include_index=False):
    """Logs the each element in collection to the console.

    :param collection: Collection to be iterated and output.
    :param include_index: Determines if index is also output.
    :return: None
    """
    try:
        # Iterate by converting to enumeration.
        for index, item in enumerate(collection):
            if include_index:
                Logging.log(f'collection[{index}]: {item}')
            else:
                Logging.log(item)
    except IndexError as error:
        # Output expected IndexErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)


if __name__ == "__main__":
    main()
Posted by: Guest on April-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language