Skip to content

Commit

Permalink
Don't instantiate new CurrentThreadScheduler._Local with each schedul…
Browse files Browse the repository at this point in the history
…er instance
  • Loading branch information
erikkemperman committed Apr 28, 2019
1 parent be24259 commit 2f33805
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions rx/concurrency/currentthreadscheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ class CurrentThreadScheduler(SchedulerBase):
You never want to schedule timeouts using the CurrentThreadScheduler since
that will block the current thread while waiting.
"""

_global: MutableMapping[threading.Thread, 'CurrentThreadScheduler'] = WeakKeyDictionary()

class _Local(threading.local):
__slots__ = 'idle', 'queue'

def __init__(self):
self.idle: bool = True
self.queue: PriorityQueue[ScheduledItem[typing.TState]] = PriorityQueue()

_local = _Local()
_global: MutableMapping[threading.Thread, 'CurrentThreadScheduler'] = WeakKeyDictionary()

def __new__(cls) -> 'CurrentThreadScheduler':
"""Ensure that each thread has at most a single instance."""

Expand All @@ -59,8 +59,7 @@ def __new__(cls) -> 'CurrentThreadScheduler':
def __init__(self) -> None:
"""Creates a scheduler that schedules work as soon as possible
on the current thread."""

self._local = CurrentThreadScheduler._Local()
super().__init__()

def schedule(self,
action: typing.ScheduledAction,
Expand Down Expand Up @@ -114,7 +113,7 @@ def schedule_absolute(self, duetime: typing.AbsoluteTime,

si: ScheduledItem[typing.TState] = ScheduledItem(self, state, action, duetime)

local: CurrentThreadScheduler._Local = self._local
local: CurrentThreadScheduler._Local = CurrentThreadScheduler._local
local.queue.enqueue(si)
if local.idle:
local.idle = False
Expand All @@ -134,7 +133,7 @@ def schedule_required(self) -> bool:
False; otherwise, if the trampoline is not active, then it
returns True.
"""
return self._local.idle
return CurrentThreadScheduler._local.idle

def ensure_trampoline(self, action):
"""Method for testing the CurrentThreadScheduler."""
Expand Down

0 comments on commit 2f33805

Please sign in to comment.