
from time import sleep
from concurrent.futures import Future, ThreadPoolExecutor
from threading import Thread
from collections import deque
from requests import Session
from datetime import datetime, timedelta
from functools import partial

class Scheduler(object):
    """Scheduler provides async interface for making VK API calls and
    incapsulates time management for queries.
    """

    def __init__(self, max_requests_per_second=3):
        self.last_requests = deque([datetime.min] * max_requests_per_second)
        self.max_requests_per_second = max_requests_per_second
        self.executor = ThreadPoolExecutor(max_workers=1)

    def _wait_and_execute(self, callback):
        first_request = self.last_requests.pop()
        now = datetime.now()
        delay = max(0, 1 - (now - first_request).total_seconds())
        sleep(delay)
        self.last_requests.appendleft(now)
        return callback()

    def __del__(self):
        self.executor.shutdown()

    def call(self, fn, *args, **kwargs):
        callback = partial(fn, *args, **kwargs)
        result = self.executor.submit(self._wait_and_execute, callback)
        return FutureFunctor.wrap(result, self.executor)


class FutureFunctor(Future):
    @staticmethod
    def wrap(future, executor):
        future.__class__ = FutureFunctor
        future.executor = executor
        return future

    def __init__(self, executor):
        self.executor = executor
        Future.__init__(self)

    def fmap(self, func):
        result = self.executor.submit(func, self.result())
        return FutureFunctor.wrap(result, self.executor)


@@ -1376,185 +1376,464 @@
     
-def __init__(self, executor):%0A        self.executor = executor%0A        Future.__init__(self)%0A%0A    def fmap(self, func):%0A        result = self.executor.submit(func, self.result()
+@staticmethod%0A    def lift(value, executor=None):%0A        result = FutureFunctor(executor)%0A        result.set_result(value)%0A        return result%0A%0A    def __init__(self, executor):%0A        self.executor = executor%0A        Future.__init__(self)%0A%0A    def fmap(self, func):%0A        def callback():%0A            return func(self.result())%0A        if self.executor:%0A            result = self.executor.submit(callback)%0A        else:%0A            result = callback(
 )%0A  

Add lift method to FutureFunctor