Skip to content

Commit 95b4c0a

Browse files
committed
allow caching custom function
1 parent 5e37bac commit 95b4c0a

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* Allow caching custom function to cloud.
13+
1214
### Changed
1315

1416
### Removed

examples/cache_function.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from compas_cloud import Proxy
2+
from compas.geometry import Point
3+
4+
5+
p = Proxy()
6+
7+
def move_point(point):
8+
from compas.geometry import Translation
9+
return point.transformed(Translation.from_vector([1, 0, 0]))
10+
11+
12+
# save the custom function to cloud
13+
# cache=True means the function will return a cache object instead of the actual data
14+
move_point_cloud = p.function(move_point, cache=True)
15+
16+
# Cache the input object to cloud
17+
point = Point(0, 0, 0)
18+
point_cache = p.cache(point)
19+
20+
# Call the function on cloud
21+
# In this case, both input and output are cached objects, which is not sent back to client unless explicitly requested
22+
result_cache = move_point_cloud(point_cache)
23+
print(result_cache)
24+
result = p.get(result_cache)
25+
print(result)
26+
27+
# The cached output object can then be used as input directly on cloud
28+
for i in range(3):
29+
result_cache = move_point_cloud(result_cache)
30+
print(result_cache)
31+
result = p.get(result_cache)
32+
print(result)
33+

src/compas_cloud/proxy.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,23 @@ def __init__(self, host='127.0.0.1', port=9009, background=True, errorHandler=No
119119
def package(self, function, cache=False):
120120
raise RuntimeError("Proxy.package() has been deprecated, please use Proxy.function() instead.")
121121

122-
def function(self, function, cache=False):
122+
def function(self, package, cache=False):
123123
"""returns wrapper of function that will be executed on server side"""
124124

125+
if callable(package):
126+
package = self.cache(package)
127+
125128
if self.errorHandler:
126129
@self.errorHandler
127130
@retry_if_exception(Exception, 5, wait=0.5)
128131
def run_function(*args, **kwargs):
129-
return self.run(function, cache, *args, **kwargs)
132+
return self.run(package, cache, *args, **kwargs)
130133

131134
return run_function
132135
else:
133136
@retry_if_exception(Exception, 5, wait=0.5)
134137
def run_function(*args, **kwargs):
135-
return self.run(function, cache, *args, **kwargs)
138+
return self.run(package, cache, *args, **kwargs)
136139

137140
return run_function
138141

src/compas_cloud/server.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ def load_cached(self, data):
6666
def execute(self, data):
6767
"""execute corresponding binded functions with received arguments"""
6868
package = data['package']
69-
names = package.split('.')
70-
name = '.'.join(names[:-1])
71-
module = importlib.import_module(name)
72-
function = getattr(module, names[-1])
69+
70+
if isinstance(package, dict):
71+
function = self.cached[package['cached_func']]
72+
else:
73+
names = package.split('.')
74+
name = '.'.join(names[:-1])
75+
module = importlib.import_module(name)
76+
function = getattr(module, names[-1])
7377

7478
start = time.time()
7579
print('running:', package)

0 commit comments

Comments
 (0)