Skip to content

Instantly share code, notes, and snippets.

@jbbarth
Last active November 26, 2015 15:42
Show Gist options
  • Select an option

  • Save jbbarth/a980562e006c2479f4a4 to your computer and use it in GitHub Desktop.

Select an option

Save jbbarth/a980562e006c2479f4a4 to your computer and use it in GitHub Desktop.
simpleflow classes support
diff --git a/examples/basic.py b/examples/basic.py
index 1b5e538..f431c59 100644
--- a/examples/basic.py
+++ b/examples/basic.py
@@ -17,10 +17,18 @@ def double(x):
return x * 2
+# simpleflow activities can be classes ; in that case the class is instantiated
+# with the params passed via submit, then the `execute()` method is called and
+# the result is returned.
@activity.with_attributes(task_list='quickstart', version='example')
-def delay(t, x):
- time.sleep(t)
- return x
+class Delay(object):
+ def __init__(self, t, x):
+ self.t = t
+ self.x = x
+
+ def execute(self):
+ time.sleep(self.t)
+ return self.x
class BasicWorkflow(Workflow):
@@ -30,7 +38,7 @@ class BasicWorkflow(Workflow):
def run(self, x, t=30):
y = self.submit(increment, x)
- yy = self.submit(delay, t, y)
+ yy = self.submit(Delay, t, y)
z = self.submit(double, y)
print '({x} + 1) * 2 = {result}'.format(
diff --git a/simpleflow/local/executor.py b/simpleflow/local/executor.py
index 0f81983..734ac11 100644
--- a/simpleflow/local/executor.py
+++ b/simpleflow/local/executor.py
@@ -23,8 +23,12 @@ class Executor(executor.Executor):
key, val in kwargs.iteritems()}
future = futures.Future()
+ handler = func._callable
try:
- future._result = func._callable(*args, **kwargs)
+ if hasattr(handler, 'execute'):
+ future._result = handler(*args, **kwargs).execute()
+ else:
+ future._result = handler(*args, **kwargs)
except Exception as err:
future._exception = err
if func.raises_on_failure:
diff --git a/simpleflow/swf/process/worker/base.py b/simpleflow/swf/process/worker/base.py
index b45e1bb..57afe70 100644
--- a/simpleflow/swf/process/worker/base.py
+++ b/simpleflow/swf/process/worker/base.py
@@ -106,7 +106,10 @@ class ActivityWorker(object):
args = input.get('args', ())
kwargs = input.get('kwargs', {})
try:
- result = handler(*args, **kwargs)
+ if hasattr(handler, 'execute'):
+ result = handler(*args, **kwargs).execute()
+ else:
+ result = handler(*args, **kwargs)
except Exception as err:
tb = traceback.format_exc()
logger.exception(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment