summaryrefslogtreecommitdiff
path: root/taskflow/utils/async_utils.py
blob: b055a27bda82ceb119a59d2287b42a295bd4e4ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-

#    Copyright (C) 2013 Yahoo! Inc. All Rights Reserved.
#
#    Licensed 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.

from concurrent import futures as _futures
from concurrent.futures import _base

try:
    from eventlet.green import threading as greenthreading
    EVENTLET_AVAILABLE = True
except ImportError:
    EVENTLET_AVAILABLE = False

from taskflow.types import futures


_DONE_STATES = frozenset([
    _base.CANCELLED_AND_NOTIFIED,
    _base.FINISHED,
])


def make_completed_future(result, exception=False):
    """Make a future completed with a given result."""
    future = futures.Future()
    if exception:
        future.set_exception(result)
    else:
        future.set_result(result)
    return future


def wait_for_any(fs, timeout=None):
    """Wait for one of the futures to complete.

    Works correctly with both green and non-green futures (but not both
    together, since this can't be guaranteed to avoid dead-lock due to how
    the waiting implementations are different when green threads are being
    used).

    Returns pair (done futures, not done futures).
    """
    green_fs = sum(1 for f in fs if isinstance(f, futures.GreenFuture))
    if not green_fs:
        return tuple(_futures.wait(fs, timeout=timeout,
                                   return_when=_futures.FIRST_COMPLETED))
    else:
        non_green_fs = len(fs) - green_fs
        if non_green_fs:
            raise RuntimeError("Can not wait on %s green futures and %s"
                               " non-green futures in the same `wait_for_any`"
                               " call" % (green_fs, non_green_fs))
        else:
            return _wait_for_any_green(fs, timeout=timeout)


class _GreenWaiter(object):
    """Provides the event that wait_for_any() blocks on."""
    def __init__(self):
        self.event = greenthreading.Event()

    def add_result(self, future):
        self.event.set()

    def add_exception(self, future):
        self.event.set()

    def add_cancelled(self, future):
        self.event.set()


def _wait_for_any_green(fs, timeout=None):
    assert EVENTLET_AVAILABLE, 'eventlet is needed to wait on green futures'

    def _partition_futures(fs):
        done = set()
        not_done = set()
        for f in fs:
            if f._state in _DONE_STATES:
                done.add(f)
            else:
                not_done.add(f)
        return (done, not_done)

    with _base._AcquireFutures(fs):
        (done, not_done) = _partition_futures(fs)
        if done:
            return (done, not_done)
        waiter = _GreenWaiter()
        for f in fs:
            f._waiters.append(waiter)

    waiter.event.wait(timeout)
    for f in fs:
        f._waiters.remove(waiter)

    with _base._AcquireFutures(fs):
        return _partition_futures(fs)