natekupp / ffx

Fast Function Extraction
http://trent.st/ffx
Other
80 stars 97 forks source link

SIGALRM on windows #17

Closed saarahy closed 7 years ago

saarahy commented 8 years ago

hey! i'm using this module on windows and when i try to run the example, it show me this error:

'module' object has no attribute 'SIGALRM'

i know that this function it's only available on unix, do you have an equivalent for windows?

thnks!

jmmcd commented 8 years ago

Thanks for this. I'm afraid I don't have any Windows machines, nor Windows expertise, so I won't be able to address this :-/

AnnanFay commented 8 years ago

The code below is quite painful to read through, and was seriously painful to write. But it seems to work somewhat. I do not want it merged with the main code base, however I'm posting it here as a starting point if someone wants to expand on it.

This question + answer provides some interesting reading on the subject

The code below should be inserted at line 924 of core.py and the above lines for timeout and ElasticNetWithTimeout commented out.

import multiprocessing
# Using a global process pool sucks, and we need
# it to start off blank so it only gets initialised 
# in the main thread. Still, this is much better 
# than recreating processes.
timeout_pool = None

def timeout_using_process_pool(seconds_before_timeout):

    def decorate(f):
        def new_f(*args, **kwargs):
            global timeout_pool
            if timeout_pool is None:
                timeout_pool = multiprocessing.Pool(1)

            res = timeout_pool.apply_async(f, args, kwargs)

            try:
                return res.get(timeout=seconds_before_timeout)
            except multiprocessing.TimeoutError:
                raise TimeoutError()
        new_f.func_name = f.func_name
        return new_f
    return decorate

# this needs to be a module function so it can be pickled
def elastic_net_fit_call(self, *args, **kwargs):
    return ElasticNet.fit(self, *args, **kwargs)

elastic_net_fit_call_with_timeout = timeout_using_process_pool(MAX_TIME_REGULARIZE_UPDATE)(elastic_net_fit_call)

# insert our above functions into the format from the original code
class ElasticNetWithTimeout(ElasticNet):
    def fit(self, *args, **kwargs):
        #if this freezes, then exit with a TimeoutError

        v = elastic_net_fit_call_with_timeout(self, *args, **kwargs)

        # since our multiprocess calls pickle everything,
        # the above doesn't update `self`
        # luckily v is the new self, which we can manually update
        self.__dict__.update(v.__dict__)
        return v
yukoba commented 8 years ago

I added this code. This is just ignoring timeout. https://github.com/yukoba/ffx/commit/69af2052454c01cb3c556d48508932b1f0ce249a

def timeout(seconds_before_timeout):
    def decorate(f):
        if not hasattr(signal, "SIGALRM"):
            return f

If someone needs this, use this branch. https://github.com/yukoba/ffx/tree/windows