Skip to content

Commit

Permalink
Merge pull request #37 from syrusakbary/features/no-context-and-sched…
Browse files Browse the repository at this point in the history
…ulers-refactor

Schedulers refactor
  • Loading branch information
syrusakbary authored Jul 14, 2017
2 parents 49f3509 + 50902a9 commit 5177e53
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 418 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ language: python
sudo: false
python:
- 2.7
- 3.3
- 3.4
- 3.5
- 3.6
- pypy
before_install:
- |
Expand Down
9 changes: 2 additions & 7 deletions promise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
get_default_scheduler,
set_default_scheduler
)
from .scheduler import SyncScheduler
try:
from .thread_sheduler import ThreadScheduler
except ImportError:
ThreadScheduler = None
from .schedulers.immediate import ImmediateScheduler

__all__ = [
'Promise',
Expand All @@ -38,6 +34,5 @@
'async_instance',
'get_default_scheduler',
'set_default_scheduler',
'SyncScheduler',
'ThreadScheduler'
'ImmediateScheduler'
]
32 changes: 13 additions & 19 deletions promise/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class Async(object):

def __init__(self, schedule):
self.is_tick_used = False
self.late_queue = collections.deque()
self.normal_queue = collections.deque()
self.late_queue = collections.deque() # type: ignore
self.normal_queue = collections.deque() # type: ignore
self.have_drained_queues = False
self.trampoline_enabled = True
self.trampoline_enabled = False
self.schedule = schedule

def enable_trampoline(self):
Expand All @@ -21,27 +21,27 @@ def disable_trampoline(self):
def have_items_queued(self):
return self.is_tick_used or self.have_drained_queues

def _async_invoke_later(self, fn, context):
def _async_invoke_later(self, fn):
self.late_queue.append(fn)
self.queue_tick(context)
self.queue_tick()

def _async_invoke(self, fn, context):
def _async_invoke(self, fn):
self.normal_queue.append(fn)
self.queue_tick(context)
self.queue_tick()

def _async_settle_promise(self, promise):
self.normal_queue.append(promise)
self.queue_tick(context=promise._trace)
self.queue_tick()

def invoke_later(self, fn, context):
def invoke_later(self, fn):
if self.trampoline_enabled:
self._async_invoke_later(fn, context)
self._async_invoke_later(fn)
else:
self.schedule.call_later(0.1, fn)

def invoke(self, fn, context):
def invoke(self, fn):
if self.trampoline_enabled:
self._async_invoke(fn, context)
self._async_invoke(fn)
else:
self.schedule.call(
fn
Expand Down Expand Up @@ -79,16 +79,10 @@ def drain_queues(self):
self.have_drained_queues = True
self.drain_queue(self.late_queue)

def queue_context_tick(self):
def queue_tick(self):
if not self.is_tick_used:
self.is_tick_used = True
self.schedule.call(self.drain_queues)

def queue_tick(self, context):
if not context:
self.queue_context_tick()
else:
(context._parent or context).on_exit(self.queue_context_tick)

def reset(self):
self.is_tick_used = False
58 changes: 0 additions & 58 deletions promise/context.py

This file was deleted.

17 changes: 1 addition & 16 deletions promise/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import List, Sized # flake8: noqa

from .promise import Promise, async_instance
from .context import Context


def get_chunks(iterable_obj, chunk_size=1):
Expand Down Expand Up @@ -180,25 +179,11 @@ def prime(self, key, value):
# Private: cached resolved Promise instance
resolved_promise = None

# def enqueue_post_promise_job(fn):
# # t.run()
# # from threading import Timer
# # t = Timer(0.10, fn)
# # t.run()
# # return fn()
# global resolved_promise
# if not resolved_promise:
# resolved_promise = Promise.resolve(None)
# resolved_promise.then(lambda v: queue.invoke(fn)) # TODO: Change to async

def enqueue_post_promise_job(fn):
global resolved_promise
if not resolved_promise:
resolved_promise = Promise.resolve(None)
# queue.invoke(fn)
async_instance.invoke(fn, context=Context.peek_context())
# Promise.resolve(None).then(lambda v: async.invoke(fn, context=Context.peek_context()))
# resolved_promise.then(lambda v: queue.invoke(fn, context=Context.peek_context()))
resolved_promise.then(lambda v: async_instance.invoke(fn))


def dispatch_queue(loader):
Expand Down
Loading

0 comments on commit 5177e53

Please sign in to comment.