Skip to content

Commit

Permalink
need to use the old generic_parent (now mainloop) for all switches, n…
Browse files Browse the repository at this point in the history
…ot just for dead greenlets
  • Loading branch information
teepark committed Jan 10, 2010
1 parent b01f3a9 commit 92dac2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
26 changes: 13 additions & 13 deletions greenhouse/scheduler.py
Expand Up @@ -10,8 +10,8 @@
from greenhouse.compat import greenlet


__all__ = ["get_next", "pause", "pause_until", "pause_for", "schedule",
"schedule_at", "schedule_in", "schedule_recurring"]
__all__ = ["pause", "pause_until", "pause_for", "schedule", "schedule_at",
"schedule_in", "schedule_recurring"]

# these are intended to be monkey-patchable from client code
PRINT_EXCEPTIONS = True
Expand Down Expand Up @@ -49,7 +49,7 @@ def _repopulate():
state.to_run.extend(p[1] for p in state.timed_paused[:index])
state.timed_paused = state.timed_paused[index:]

def get_next():
def _get_next():
'update the scheduler state and figure out the next greenlet to run'
if not state.to_run:
_repopulate()
Expand All @@ -68,13 +68,13 @@ def get_next():
def pause():
'pause and reschedule the current greenlet and switch to the next'
schedule(greenlet.getcurrent())
get_next().switch()
mainloop.switch()

def pause_until(unixtime):
'''pause and reschedule the current greenlet until a set time,
then switch to the next'''
schedule_at(unixtime, greenlet.getcurrent())
get_next().switch()
mainloop.switch()

def pause_for(secs):
'''pause and reschedule the current greenlet for a set number of seconds,
Expand All @@ -97,7 +97,7 @@ def decorator(target):
inner_target = target
def target():
inner_target(*args, **(kwargs or {}))
glet = greenlet(target, state.generic_parent)
glet = greenlet(target, state.mainloop)
state.paused.append(glet)
return target

Expand All @@ -118,7 +118,7 @@ def decorator(target):
inner_target = target
def target():
inner_target(*args, **kwargs)
glet = greenlet(target, state.generic_parent)
glet = greenlet(target, state.mainloop)
bisect.insort(state.timed_paused, (unixtime, glet))
return target

Expand Down Expand Up @@ -170,21 +170,21 @@ def run_and_schedule_one(tstamp, count):
return target

@greenlet
def generic_parent(ended):
def mainloop():
while 1:
if not traceback or not state: #pragma: no cover
# python's shutdown sequence gets out of wack when we have
# greenlets in play. in certain circumstances, modules become
# None before this code runs.
break
try:
get_next().switch()
_get_next().switch()
except Exception, exc:
if PRINT_EXCEPTIONS: #pragma: no cover
traceback.print_exception(*sys.exc_info(), file=EXCEPTION_FILE)
state.generic_parent = generic_parent
state.mainloop = mainloop

# rig it so the next get_next() call will definitely put us right back here
# rig it so the next _get_next() call will definitely put us right back here
state.to_run.appendleft(greenlet.getcurrent())

# then prime the pump. if there is a traceback before the generic parent
Expand All @@ -193,7 +193,7 @@ def generic_parent(ended):
@schedule
def f():
pass
get_next().switch()
mainloop.switch()

def hybridize():
'''change the process-global scheduler state to be thread-local
Expand All @@ -219,5 +219,5 @@ def hybridize():
state.state.paused = procstate.paused
state.state.descriptormap = procstate.descriptormap
state.state.to_run = procstate.to_run
state.state.generic_parent = procstate.generic_parent
state.state.mainloop = procstate.mainloop
greenhouse.poller.set()
16 changes: 8 additions & 8 deletions greenhouse/utils.py
Expand Up @@ -91,7 +91,7 @@ def hit_timeout():
current.switch()

self._waiters.append(current)
scheduler.get_next().switch()
state.mainloop.switch()

if current in self._awoken_by_timeout:
self._awoken_by_timeout.remove(current)
Expand Down Expand Up @@ -128,7 +128,7 @@ def acquire(self, blocking=True):
return not locked_already
if self._locked:
self._waiters.append(greenlet.getcurrent())
scheduler.get_next().switch()
state.mainloop.switch()
self._locked = True
return True

Expand Down Expand Up @@ -169,7 +169,7 @@ def acquire(self, blocking=True):
return False
if self._locked:
self._waiters.append(greenlet.getcurrent())
scheduler.get_next().switch()
state.mainloop.switch()
self._owner = current
self._locked = True
self._count = 1
Expand Down Expand Up @@ -227,7 +227,7 @@ def hit_timeout():
self._waiters.remove(current)
current.switch()

scheduler.get_next().switch()
state.mainloop.switch()
self._lock.acquire()

def notify(self, num=1):
Expand Down Expand Up @@ -266,7 +266,7 @@ def acquire(self, blocking=True):
elif not blocking:
return False
self._waiters.append(greenlet.getcurrent())
scheduler.get_next().switch()
state.mainloop.switch()
return True

def release(self):
Expand Down Expand Up @@ -304,7 +304,7 @@ def __init__(self, secs, func, args=(), kwargs=None):
self.args = args
self.kwargs = kwargs

self._glet = glet = greenlet(self.func, state.generic_parent)
self._glet = glet = greenlet(self.func, state.mainloop)

self.waketime = waketime = time.time() + secs
self.cancelled = False
Expand Down Expand Up @@ -505,7 +505,7 @@ def receive(self):
return item
else:
self._waiters.append(greenlet.getcurrent())
scheduler.get_next().switch()
state.mainloop.switch()
return self._dataqueue.pop()

next = receive
Expand All @@ -521,4 +521,4 @@ def send(self, item):
else:
self._dataqueue.append(item)
self._waiters.append(greenlet.getcurrent())
scheduler.get_next().switch()
state.mainloop.switch()

0 comments on commit 92dac2d

Please sign in to comment.