executor.py 3.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17 18 19 20 21 22 23 24
""" Abstraction for asynchronous job execution """

class Executor(object):
    """
    Base abstract executor interface for asynchronous job submission.
    Allows submit asynchronous jobs and returns the Future object.
    """
    # timeout for jobs that may hang
25
    DEFAULT_TIMEOUT = 120
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    def submit(self, func, *args, **kwargs):
        """
        Pass task (function, arguments) to the Executor.

        Parameters
        ----------
        func : callable
            function to be run by a worker
        args : list or tuple, optional
            arguments passed to the function
        kwargs : dict, optional
            The keyword arguments

        Returns
        -------
        future : Future
            Future object wrapping the task which can be used to
            collect the task's result.
        """
        raise NotImplementedError()


class Future(object):
    """
    Base class of the future object.
    The implementations can return object of subclass of this.
    This objects encapsulates the asynchronous execution of task
    submitted to another thread, or another worker for execution.

    Future objects store the state of tasks--can be polled for
    result or a blocking call to retrieve the result can be used.
    """
    def done(self):
        """
        Return True if job was successfully cancelled or finished running.
        """
        raise NotImplementedError()

    def get(self, timeout=None):
        """
        Get the result. This will block until the result is available.

        Parameters
        ----------
        timeout : int or float, optional
            Maximum number of seconds to wait before it timeouts.
            If not specified, it means we block until the result is available.

        Returns
        -------
        result : Any
            The result returned by the submitted function.

        Raises
        ------
        TimeoutError : if the result call timeouts.
        """
        raise NotImplementedError()

class FutureError(RuntimeError):
    """Base error class of all future events"""
88

89 90 91 92

# pylint:disable=redefined-builtin
class TimeoutError(FutureError):
    """Error raised when a task is timeout."""
93

94 95 96 97 98

class ExecutionError(FutureError):
    """
    Error raised when future execution crashes or failed.
    """