with-timeout

Macro

Package: mp

Arguments: (seconds &body timeout-body) &body body

This macro evaluates the body as a progn body. If the evaluation of body does not complete within the specified interval, execution throws out of the body and the timeout-body forms are evaluated as a progn body, returning the result of the last form. The timeout-body forms are not evaluated if the body completes within seconds.

The value of seconds may be any real number. Negative values are interpreted as 0. This macro uses operating system utilities for timing so its granularity depends on the granularity of the operating system functions. We cannot in this manual specify the granularity you will see (since the manual is generic over many systems and environmental factors affect the result) but we can say that seconds of less than about 0.08 will always time out on most Unix systems. We strongly recommend that you test this macro to find out the granularity on your system.

Here is an example of code using mp:with-timeout. We define a variant of y-or-n-p which times out after a specified number of seconds, taking the default value specified in the arguments.

(defun y-or-n-p-with-timeout (seconds default &rest args)
  (mp:with-timeout (seconds (format t "[Timed out] ~a~%" default)
             default)
         (apply #'y-or-n-p args)))

See multiprocessing.htm for general information on multiprocessing in Allegro CL.

The documentation is described in introduction.htm and the index is in index.htm.

Copyright (c) 1998-2000, Franz Inc. Berkeley, CA., USA. All rights reserved.

Created 2000.10.5.