File: immediate.py

package info (click to toggle)
python-promise 2.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 400 kB
  • sloc: python: 2,681; sh: 13; makefile: 4
file content (27 lines) | stat: -rw-r--r-- 663 bytes parent folder | download | duplicates (2)
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
from threading import Event

if False:
    from ..promise import Promise
    from typing import Callable, Any, Optional  # flake8: noqa


class ImmediateScheduler(object):
    def call(self, fn):
        # type: (Callable) -> None
        try:
            fn()
        except:
            pass

    def wait(self, promise, timeout=None):
        # type: (Promise, Optional[float]) -> None
        e = Event()

        def on_resolve_or_reject(_):
            # type: (Any) -> None
            e.set()

        promise._then(on_resolve_or_reject, on_resolve_or_reject)
        waited = e.wait(timeout)
        if not waited:
            raise Exception("Timeout")