# Path: asynq/scheduler.py
# class AsyncTaskError(Exception):
# class TaskScheduler(object):
# class LocalTaskSchedulerState(threading.local):
#     def __init__(self):
#     def reset(self):
#     def wait_for(self, task):
#     def _execute(self, root_task):
#     def _schedule_batch(self, batch):
#     def _flush_batch(self, batch):
#     def _handle_async_task(self, task):
#     def _continue_with_task(self, task):
#     def _continue_with_batch(self):
#     def _select_batch_to_flush(self):
#     def __str__(self):
#     def __repr__(self):
#     def dump(self, indent=0):
#     def try_time_based_dump(self, last_task=None):
#     def __init__(self):
#     def reset(self):
# def get_scheduler():
# def reset():
# def get_active_task():
# 
# Path: asynq/tests/caching.py
# class ExternalCacheBase(CacheBase):
#     """
#     Abstract base class for external caches (memcached, etc.).
#     """
# 
#     def __init__(self):
#         super(ExternalCacheBase, self).__init__()
#         self._local = LocalCache()
#         self._batch = ExternalCacheBatch(self, 1)
#         self.use_local_cache = True
# 
#     def get(self, key):
#         self._dependency(key)
#         value = self._local.get(key)
#         if value is miss:  # No item is cached locally
#             return self._create_batch_item("get", key)
#         return ConstFuture(miss if value is none else value)
# 
#     def set(self, key, value):
#         self._dependency(key)
#         if self.use_local_cache:
#             self._local.set(key, none if value is miss else value)
#         return self._create_batch_item("set", key, value)
# 
#     def clear(self):
#         self.clear_local_cache()
#         return self._create_batch_item("clear")
# 
#     def flush(self):
#         return self._batch.flush()
# 
#     def cancel_flush(self):
#         return self._batch.cancel()
# 
#     def clear_local_cache(self):
#         self._local.clear()
# 
#     def _before_flush(self, batch):
#         pass
# 
#     def _flush(self, batch):
#         # Actual batch flush implementation
#         raise NotImplementedError()
# 
#     def _cancel_flush(self, batch):
#         # Actual batch cancel implementation
#         pass
# 
#     def _create_batch_item(self, operation, *args):
#         return ExternalCacheBatchItem(self._batch, operation, *args)
# 
# class LocalCache(CacheBase):
#     """
#     Local cache implementation (dictionary-based).
#     Since all operations can be performed immediately
#     by this case, it never returns futures (so it doesn't
#     employ batching as well).
#     """
# 
#     def __init__(self):
#         super(LocalCache, self).__init__()
#         self._storage = {}
# 
#     def get(self, key):
#         self._dependency(key)
#         return self._storage.get(key, miss)
# 
#     def set(self, key, value):
#         self._dependency(key)
#         if value is miss:
#             if key in self._storage:
#                 del self._storage[key]
#         else:
#             self._storage[key] = value
#         return
# 
#     def clear(self):
#         self._storage.clear()
#         return


from qcore import none, miss
from asynq import scheduler
from .caching import ExternalCacheBase, LocalCache

    def _flush(self, batch):
        print("%s batch %i:" % (self.name, batch.index))
        for item in batch.items:
            print("  %s" % str(item))
            if item.is_computed():
                # Item can be computed due to task cancellation
                continue
            try:
                method = self.__getattribute__("_flush_" + item.operation)
                item.set_value(method(item, *item.args))
            except BaseException as e:
                item.set_error(e)
                # No need to re-throw, since user will anyway
                # see it by accessing the value

    def _cancel_flush(self, batch):
        print("%s batch %i is cancelled." % (self.name, batch.index))

    def _flush_get(self, item, key):
        value = self._remote.get(key)
        if self.use_local_cache:
            self._local.set(key, none if value is miss else value)
        return value

    def _flush_set(self, item, key, value):
        self._remote.set(key, value)
        return

    def _flush_clear(self, item):

# Path: asynq/scheduler.py
# class AsyncTaskError(Exception):
# class TaskScheduler(object):
# class LocalTaskSchedulerState(threading.local):
#     def __init__(self):
#     def reset(self):
#     def wait_for(self, task):
#     def _execute(self, root_task):
#     def _schedule_batch(self, batch):
#     def _flush_batch(self, batch):
#     def _handle_async_task(self, task):
#     def _continue_with_task(self, task):
#     def _continue_with_batch(self):
#     def _select_batch_to_flush(self):
#     def __str__(self):
#     def __repr__(self):
#     def dump(self, indent=0):
#     def try_time_based_dump(self, last_task=None):
#     def __init__(self):
#     def reset(self):
# def get_scheduler():
# def reset():
# def get_active_task():
# 
# Path: asynq/tests/caching.py
# class ExternalCacheBase(CacheBase):
#     """
#     Abstract base class for external caches (memcached, etc.).
#     """
# 
#     def __init__(self):
#         super(ExternalCacheBase, self).__init__()
#         self._local = LocalCache()
#         self._batch = ExternalCacheBatch(self, 1)
#         self.use_local_cache = True
# 
#     def get(self, key):
#         self._dependency(key)
#         value = self._local.get(key)
#         if value is miss:  # No item is cached locally
#             return self._create_batch_item("get", key)
#         return ConstFuture(miss if value is none else value)
# 
#     def set(self, key, value):
#         self._dependency(key)
#         if self.use_local_cache:
#             self._local.set(key, none if value is miss else value)
#         return self._create_batch_item("set", key, value)
# 
#     def clear(self):
#         self.clear_local_cache()
#         return self._create_batch_item("clear")
# 
#     def flush(self):
#         return self._batch.flush()
# 
#     def cancel_flush(self):
#         return self._batch.cancel()
# 
#     def clear_local_cache(self):
#         self._local.clear()
# 
#     def _before_flush(self, batch):
#         pass
# 
#     def _flush(self, batch):
#         # Actual batch flush implementation
#         raise NotImplementedError()
# 
#     def _cancel_flush(self, batch):
#         # Actual batch cancel implementation
#         pass
# 
#     def _create_batch_item(self, operation, *args):
#         return ExternalCacheBatchItem(self._batch, operation, *args)
# 
# class LocalCache(CacheBase):
#     """
#     Local cache implementation (dictionary-based).
#     Since all operations can be performed immediately
#     by this case, it never returns futures (so it doesn't
#     employ batching as well).
#     """
# 
#     def __init__(self):
#         super(LocalCache, self).__init__()
#         self._storage = {}
# 
#     def get(self, key):
#         self._dependency(key)
#         return self._storage.get(key, miss)
# 
#     def set(self, key, value):
#         self._dependency(key)
#         if value is miss:
#             if key in self._storage:
#                 del self._storage[key]
#         else:
#             self._storage[key] = value
#         return
# 
#     def clear(self):
#         self._storage.clear()
#         return


# Path: asynq/tests/debug_cache.py
from qcore import none, miss
from asynq import scheduler
from .caching import ExternalCacheBase, LocalCache


    def _flush(self, batch):
        print("%s batch %i:" % (self.name, batch.index))
        for item in batch.items:
            print("  %s" % str(item))
            if item.is_computed():
                # Item can be computed due to task cancellation
                continue
            try:
                method = self.__getattribute__("_flush_" + item.operation)
                item.set_value(method(item, *item.args))
            except BaseException as e:
                item.set_error(e)
                # No need to re-throw, since user will anyway
                # see it by accessing the value

    def _cancel_flush(self, batch):
        print("%s batch %i is cancelled." % (self.name, batch.index))

    def _flush_get(self, item, key):
        value = self._remote.get(key)
        if self.use_local_cache:
            self._local.set(key, none if value is miss else value)
        return value

    def _flush_set(self, item, key, value):
        self._remote.set(key, value)
        return

    def _flush_clear(self, item):

        self._remote.clear()