Skip to main content

Timeout function if it takes too long to finish in Python

import errno
import os
import signal
import time

class TimeoutError(Exception):
    pass

class timeout:
    def __init__(self, seconds=1, error_message='Timeout'):
        self.seconds = seconds
        self.error_message = error_message
    def handle_timeout(self, signum, frame):
        raise TimeoutError(self.error_message)
    def __enter__(self):
        signal.signal(signal.SIGALRM, self.handle_timeout)
        signal.alarm(self.seconds)
    def __exit__(self, type, value, traceback):
        signal.alarm(0)


with timeout(seconds=3):
    time.sleep(4)

Comments

Popular posts from this blog

Common Sense Identification of the Security Problems

Organizations make key information security mistakes, which leads to inefficient and ineffective control environment. High profile data breaches and cyber-attacks drive the industry to look for more comprehensive protection measures since many organizations feel that their capability to withstand persistent targeted attacks is minimal. But at the same time, these organizations make some key information security mistakes, that jeopardize their efforts towards control robustness. Although many firms invest in security technologies and people, no one has the confidence that the measures taken are good enough to protect their data from compromises. Below are the 10 worst mistakes which are common to find, and important to address in the path of mature information security posture. If you analyze the cyber security scenarios, and organizational capabilities, the prevailing trend is a vendor-driven approach. In many cases, security professionals adopt the attitude of procuring...

Real-Time Talk: Windows 10 IoT Core Background Tasks and ASP.NET Core Web Apps

Display useful information from your Windows 10 IoT Core application in an ASP.NET Core web app, essential for integrating IoT data into a solution. Windows 10 IoT background task talk with a web application using WebSockets. Problems As my path to this solution has been troublesome, I am listing here the main problems I faced so my dear readers have a better idea of dead-end streets along the way: I was not able to make the ASP.NET Core web application run under a Windows 10 IoT background service. I found no information about when or if it will be supported in the near future. ASP.NET MVC and ASP.NET Core have different SignalR implementations. I was not able to make a SignalR client for .NET Core work with SignalR hosted on a web application. I was able to make things work by directly using a WebSocket. It’s not as nice a solution as I had in my mind, but it works until things get better. Making the Background Task and Web Application Talk I worked out sim...

Setting up a development machine on Ubuntu

These steps will guide you on setting up an Ubuntu machine as a development machine. Here is what I use the machine for... Ruby on Rails development PHP development Java development Photo management All these commands are executed in TERMINAL Basic Setup # make sure every thing is update sudo apt-get update sudo apt-get upgrade # this will install adobe-flash, sun-jre # http://packages.ubuntu.com/jaunty/ubuntu-restricted-extras # #if you are using KUBUNTU     sudo apt-get install -y kubuntu-restricted-extras #if you are using UBUNTU     sudo apt-get install -y ubuntu-restricted-extras sudo apt-get install -y firefox sudo apt-get install -y vim-gtk sudo apt-get install -y synaptic Development Stuff # will install compilers (gcc and dev libraries) sudo apt-get install -y build-essential sudo apt-get install -y pkg-config Apache sudo apt-get install -y apache2 sudo apt-get install -y apache2-prefork-dev Mysql & PHP sudo apt-get...