STINNER Victor <victor.stin...@haypocalc.com> added the comment:

alarm() is one possible implementation, but Charles-François listed some 
drawbacks.

You can also use resource.setrlimit(RLIMIT_CPU), but the timeout is the CPU 
time (e.g. you cannot stop a sleep) and it is not portable (e.g. resource is 
not available on Windows).

Another possible implementation is a thread. faulthandler uses an "hidden" 
thread (implemented in C): a thread ignoring all signals using pthread_sigmask. 
Python threads are not reliable for a timeout because of the GIL, and it is not 
easy to "interrupt" another thread from the "timeout" thread. For example, you 
cannot (easily) raise an exception in another thread.

> I'm not sure there's a reliable way to write such a general-purpose
> wrapper

I agree, but it doesn't mean that it is not possible :-)

I think that you should try to implement in C a thread ignoring all signals. It 
becomes more complex when you have to implement the "interrupt the current 
thread" (current thread, or maybe the thread using the operation_timeout 
context manager?) part.

I suppose that you will have to use low-level "tricks" and you will have to 
experiment your tool on different platform.

You should start this project outside CPython (as a third party module), and 
then ask for an integration when your work is well tested. You have to know 
that a module "dies" when it enters CPython: you have to wait something like 18 
months to modify it, so you have to be sure that your code is "correct" ;-)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue12410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to