From ff87d5e456d2bdb0ce5defd5904df7b99d71bfe1 Mon Sep 17 00:00:00 2001 From: blitzmann Date: Sat, 24 Oct 2015 22:39:43 -0400 Subject: [PATCH] Add `tomorrow` to repo rather than dependency --- eos/saveddata/crest.py | 2 +- eos/tomorrow.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 eos/tomorrow.py diff --git a/eos/saveddata/crest.py b/eos/saveddata/crest.py index ccb00b1f4..73ed89d85 100644 --- a/eos/saveddata/crest.py +++ b/eos/saveddata/crest.py @@ -21,7 +21,7 @@ import urllib from cStringIO import StringIO from sqlalchemy.orm import reconstructor -from tomorrow import threads +from eos.tomorrow import threads class Crest(object): diff --git a/eos/tomorrow.py b/eos/tomorrow.py new file mode 100644 index 000000000..cbdd5c5e0 --- /dev/null +++ b/eos/tomorrow.py @@ -0,0 +1,44 @@ +# https://github.com/madisonmay/Tomorrow + +from functools import wraps + +from concurrent.futures import ThreadPoolExecutor + + +class Tomorrow(): + + def __init__(self, future, timeout): + self._future = future + self._timeout = timeout + + def __getattr__(self, name): + result = self._wait() + return result.__getattribute__(name) + + def _wait(self): + return self._future.result(self._timeout) + + +def async(n, base_type, timeout=None): + def decorator(f): + if isinstance(n, int): + pool = base_type(n) + elif isinstance(n, base_type): + pool = n + else: + raise TypeError( + "Invalid type: %s" + % type(base_type) + ) + @wraps(f) + def wrapped(*args, **kwargs): + return Tomorrow( + pool.submit(f, *args, **kwargs), + timeout=timeout + ) + return wrapped + return decorator + + +def threads(n, timeout=None): + return async(n, ThreadPoolExecutor, timeout)