Asked  7 Months ago    Answers:  5   Viewed   87 times

I have a shell script that loops through a text file containing URL:s that I want to visit and take screenshots of.

All this is done and simple. The script initializes a class that when run creates a screenshot of each site in the list. Some sites take a very, very long time to load, and some might not be loaded at all. So I want to wrap the screengrabber-function in a timeout script, making the function return False if it couldn't finish within 10 seconds.

I'm content with the simplest solution possible, maybe setting a asynchronous timer that will return False after 10 seconds no matter what actually happens inside the function?



The process for timing out an operations is described in the documentation for signal.

The basic idea is to use signal handlers to set an alarm for some time interval and raise an exception once that timer expires.

Note that this will only work on UNIX.

Here's an implementation that creates a decorator (save the following code as

from functools import wraps
import errno
import os
import signal

class TimeoutError(Exception):

def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
                result = func(*args, **kwargs)
            return result

        return wraps(func)(wrapper)

    return decorator

This creates a decorator called @timeout that can be applied to any long running functions.

So, in your application code, you can use the decorator like so:

from timeout import timeout

# Timeout a long running function with the default expiry of 10 seconds.
def long_running_function1():

# Timeout after 5 seconds
def long_running_function2():

# Timeout after 30 seconds, with the error "Connection timed out"
@timeout(30, os.strerror(errno.ETIMEDOUT))
def long_running_function3():
Tuesday, June 1, 2021
answered 7 Months ago

python3 is not Python syntax, it is the Python binary itself, the thing you run to get to the interactive interpreter.

You are confusing the command line with the Python prompt. Open a console (Windows) or terminal (Linux, Mac), the same place you'd use dir or ls to explore your filesystem from the command line.

If you are typing at a >>> or In [number]: prompt you are in the wrong place, that's the Python interpreter itself and it only takes Python syntax. If you started the Python prompt from a command line, exit at this point and go back to the command line. If you started the interpreter from IDLE or in an IDE, then you need to open a terminal or console as a separate program.

Other programs that people often confuse for Python syntax; each of these is actually a program to run in your command prompt:

  • python, python2.7, python3.5, etc.
  • pip or pip3
  • virtualenv
  • ipython
  • easy_install
  • django-admin
  • conda
  • flask
  • scrapy
  • -- this is a script you need to run with python [...].
  • Any of the above together with sudo.

with many more variations possible depending on what tools and libraries you have installed and what you are trying to do.

If given arguments, you'll get a SyntaxError exception instead, but the underlying cause is the same:

>>> pip install foobar
  File "<stdin>", line 1
    pip install foobar
SyntaxError: invalid syntax
Tuesday, June 1, 2021
answered 7 Months ago

use the following to convert to a timestamp in python 2


Sunday, August 22, 2021
answered 4 Months ago

The ajax function takes a timeout parameter and you can check the status in case of error.

var call =function(){
        url: '<?php bloginfo('template_directory'); ?>/ajax/product.php',
        type: 'get',
        timeout: 400,
        error: function(x, textStatus, m) {
            if (textStatus=="timeout") {

You might want to make something a little smarter to avoid permanent calls...

From the documentation :

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

Tuesday, August 24, 2021
Remy Lebeau
answered 4 Months ago

To reload the webpage incase the loading process is taking too long you can configure pageLoadTimeout. pageLoadTimeout sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

An example (using Selenium v3.141.59 and GeckoDriver v0.24.0):

  • Code Block:

    public class pageLoadTimeout 
        public static void main(String[] args) 
            System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
            WebDriver driver=new FirefoxDriver();
            driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
              // do your other work here
            }catch(WebDriverException e){
              System.out.println("WebDriverException occured");
  • Console Output:

    1565680787633   mozrunner::runner   INFO    Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\Users\Debanjan.B\AppData\Local\Temp\rust_mozprofile.3jw3aiyfNAiQ"
    1565680826515   Marionette  INFO    Listening on port 56499
    1565680827329   Marionette  WARN    TLS certificate errors will be ignored for this session
    Aug 13, 2019 12:50:28 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Aug 13, 2019 12:50:31 PM org.openqa.selenium.remote.ErrorCodes toStatus
    WebDriverException occured
  • You can find a relevant discussion in pageLoadTimeout in Selenium not working

You can find a detailed discussion in Do we have any generic function to check if page has completely loaded in Selenium

Tuesday, August 31, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :