Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout function to the standard library #5908

Closed
vicuna opened this issue Jan 27, 2013 · 1 comment
Closed

Add a timeout function to the standard library #5908

vicuna opened this issue Jan 27, 2013 · 1 comment

Comments

@vicuna
Copy link

vicuna commented Jan 27, 2013

Original bug ID: 5908
Reporter: smimram
Status: resolved (set by @xavierleroy on 2013-01-28T08:56:37Z)
Resolution: suspended
Priority: normal
Severity: feature
Category: standard library
Monitored by: @dbuenzli

Bug description

Hi,

I have tried to program a "timeout" function in a portable way but did not succeed with the current standard library. Since this is quite a commonly used idiom, it might be worth adding such a function to the standard library.

I wanted a "timeout" function of type:

float -> ('a -> 'b) -> 'a -> 'b option

which takes a maximum number n of seconds to run, a function f, an argument x, and returns Some (f x) if the computation ends before n seconds and None otherwise. I have tried the following:

  • the simple implementation using Unix.setitimer / Unix.alarm does not work under windows because of signals implementation
  • the caml-list suggested using Gc.create_alarm which is portable, but really imprecise (we cannot predict when a gc will occur)
  • I could have used threads and Thread.kill, but unfortunately Thread.kill is not implemented with the native compiler
  • this could be achieved with monadic threads, but I would rather not change the structure of my whole program

Thanks!

@vicuna
Copy link
Author

vicuna commented Jan 28, 2013

Comment author: @xavierleroy

The bottom line of the discussion on caml-list is that such a "timeout" function cannot be implemented correctly on non-Unix platforms, esp. Windows, but also JS-of-OCaml. So, probably, what you're asking for is impossible.

There might be a way to partially emulate Unix.setitimer under Windows, creating a separate thread that posts pseudo-signals periodically. However, such pseudo-signals will not interrupt blocking I/O operations and will only be processed when the OCaml code polls for pending signals. This might be good enough for some applications, but certainly not all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant