Commit b1517b84 authored by Hardik Juneja's avatar Hardik Juneja

CMFActivity: rename MySafeFunction to JoblibSafeFunction and replace if-else...

CMFActivity: rename MySafeFunction to JoblibSafeFunction and replace if-else block with try-except-else
parent e01df29a
......@@ -38,23 +38,22 @@ try:
from sklearn.externals.joblib.format_stack import format_exc
from Products.CMFActivity.Activity.SQLJoblib import sqljoblib_hash
except ImportError:
LOG("CMFActivityBackend", WARNING, "CLASS NOT LOADED!!!")
ENABLE_JOBLIB = False
if ENABLE_JOBLIB:
class MySafeFunction(SafeFunction):
LOG("CMFActivityBackend", WARNING, "Joblib cannot be imported, support disabled")
class CMFActivityBackend(object):
pass
else:
class JoblibSafeFunction(SafeFunction):
"""Wrapper around a SafeFunction that catches any exception
The exception can be handled in CMFActivityResult.get
"""
def __init__(self, *args, **kwargs):
super(MySafeFunction, self).__init__(*args, **kwargs)
self.batch = args[0]
super(JoblibSafeFunction, self).__init__(*args, **kwargs)
def __call__(self, *args, **kwargs):
try:
return super(MySafeFunction, self).__call__(*args, **kwargs)
return super(JoblibSafeFunction, self).__call__(*args, **kwargs)
except Exception as exc:
return exc
raise exc
class CMFActivityResult(object):
def __init__(self, active_process, active_process_sig, callback):
......@@ -95,12 +94,12 @@ if ENABLE_JOBLIB:
active_process_id = self.active_process.getId()
joblib_result = None
# create a signature and convert it to integer
sig = sqljoblib_hash((MySafeFunction(batch),))
sig = sqljoblib_hash((JoblibSafeFunction(batch),))
resultDict = self.active_process.getResultDict()
if not resultDict.has_key(sig):
joblib_result = portal_activities.activate(activity='SQLJoblib',
tag="joblib_%s" % active_process_id,
active_process=self.active_process).Base_callSafeFunction(MySafeFunction(batch))
active_process=self.active_process).Base_callSafeFunction(JoblibSafeFunction(batch))
if joblib_result is None:
joblib_result = CMFActivityResult(self.active_process, sig, callback)
return joblib_result
......@@ -126,8 +125,3 @@ if ENABLE_JOBLIB:
return
register_parallel_backend('CMFActivity', CMFActivityBackend)
else:
class CMFActivityBackend(object):
pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment