Skip to content

Commit

Permalink
refactor gevent.Event sync into a context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ceteri committed Dec 20, 2013
1 parent ffc73bd commit d95d389
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
35 changes: 15 additions & 20 deletions src/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,13 @@ def pop_gen (self, *args, **kwargs):
payload, start_response, body = worker.get_response_context(args[1:])

if worker.auth_request(payload, start_response, body):
worker.init_task_event()
with worker.wrap_task_event():
# HTTP response first, then initiate long-running task
start_response('200 OK', [('Content-Type', 'text/plain')])
body.put("Bokay\r\n")
body.put(StopIteration)

# HTTP response first, then initiate long-running task
start_response('200 OK', [('Content-Type', 'text/plain')])
body.put("Bokay\r\n")
body.put(StopIteration)

self.populate(0)
worker.done_task_event()
self.populate(0)


def pop_hist (self, *args, **kwargs):
Expand All @@ -185,18 +183,15 @@ def pop_next (self, *args, **kwargs):
payload, start_response, body = worker.get_response_context(args[1:])

if worker.auth_request(payload, start_response, body):
worker.init_task_event()

# HTTP response first, then initiate long-running task
start_response('200 OK', [('Content-Type', 'text/plain')])
body.put("Bokay\r\n")
body.put(StopIteration)

current_gen = payload["current_gen"]
fitness_cutoff = payload["fitness_cutoff"]
self.next_generation(current_gen, fitness_cutoff)

worker.done_task_event()
with worker.wrap_task_event():
# HTTP response first, then initiate long-running task
start_response('200 OK', [('Content-Type', 'text/plain')])
body.put("Bokay\r\n")
body.put(StopIteration)

current_gen = payload["current_gen"]
fitness_cutoff = payload["fitness_cutoff"]
self.next_generation(current_gen, fitness_cutoff)


def pop_enum (self, *args, **kwargs):
Expand Down
9 changes: 5 additions & 4 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# https://github.com/ceteri/exelixi


from contextlib import contextmanager
from gevent import monkey, shutdown, signal, spawn, wsgi, Greenlet
from gevent.event import Event
from gevent.queue import JoinableQueue
Expand Down Expand Up @@ -127,13 +128,13 @@ def shard_config (self, *args, **kwargs):
######################################################################
## barrier pattern methods

def init_task_event (self):
@contextmanager
def wrap_task_event (self):
"""initialize a gevent.Event, to which the UnitOfWork will wait as a listener"""
self._task_event = Event()
yield


def done_task_event (self):
"""complete a gevent.Event, notifying the UnitOfWork which waited as a listener"""
# complete the Event, notifying the UnitOfWork which waited
self._task_event.set()
self._task_event = None

Expand Down

0 comments on commit d95d389

Please sign in to comment.