Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit c2d49af

Browse files
committed
Merge branch 'master' of https://github.com/mcneel/ghpython
2 parents 2fc7a3b + 3c9eb38 commit c2d49af

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

GhPython.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<ItemGroup>
156156
<EmbeddedResource Include="package\components.py" />
157157
<EmbeddedResource Include="package\__init__.py" />
158+
<EmbeddedResource Include="package\parallel.py" />
158159
<Content Include="Samples\sampleCommon.py" />
159160
<None Include="Samples\helpText.html" />
160161
<Content Include="Samples\sampleScript.py" />

package/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# GhPython package
2-
__all__ = ["components"]
2+
__all__ = ["components", "parallel"]

package/components.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def __make_function__(helper):
77
def component_function(*args, **kwargs):
8-
comp = helper.comp
8+
comp = helper.proxy.CreateInstance()
99
comp.ClearData()
1010
if args:
1111
for i, arg in enumerate(args):
@@ -30,7 +30,10 @@ def component_function(*args, **kwargs):
3030
doc.AddObject(comp, False, 0)
3131
comp.CollectData()
3232
comp.ComputeData()
33-
return helper.create_output(comp.Params)
33+
output = helper.create_output(comp.Params)
34+
comp.ClearData()
35+
doc.Dispose()
36+
return output
3437
return component_function
3538

3639

@@ -42,15 +45,15 @@ def __init__(self):
4245
class function_helper(object):
4346
def __init__(self, proxy):
4447
self.proxy = proxy
45-
self.comp = proxy.CreateInstance()
4648
self.return_type = None
4749

4850
def create_output(self, params):
49-
import collections
51+
from collections import namedtuple
5052
output_values = []
5153
for output in params.Output:
5254
data = output.VolatileData.AllData(True)
53-
v = [x.Value for x in data]
55+
#We could call Value, but ScriptVariable seems to do a better job
56+
v = [x.ScriptVariable() for x in data]
5457
if len(v)<1:
5558
output_values.append(None)
5659
elif len(v)==1:
@@ -61,8 +64,7 @@ def create_output(self, params):
6164
if self.return_type is None:
6265
names = [output.Name.lower() for output in params.Output]
6366
try:
64-
t = collections.namedtuple('Output', names, rename=True)
65-
self.return_type = t
67+
self.return_type = namedtuple('Output', names, rename=True)
6668
except:
6769
self.return_type = False
6870
if not self.return_type: return output_values

package/parallel.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import System.Threading.Tasks as tasks
2+
from System import Exception, AggregateException
3+
4+
def run(function, data_list, flatten_results = False):
5+
"""for each item in data_list execute the input function. Execution is
6+
done on as many threads as there are CPUs on the computer.
7+
Parameters:
8+
function: function to execute for each item in the data_list
9+
data_list: list, tuple, or other enumerable data structure
10+
flatten_list [opt]: if True, when results are lists of lists the
11+
results are flattened into a single list of results. If this is True,
12+
you cannot depend on the result list being the same size as the input list
13+
Returns:
14+
list of results containing the return from each call to the input function
15+
"""
16+
pieces = [(i,data) for i,data in enumerate(data_list)]
17+
results = range(len(pieces))
18+
19+
def helper(piece):
20+
i, data = piece
21+
local_result = function(data)
22+
results[i] = local_result
23+
# Run first piece serial in case there is "set up" code in the function
24+
# that needs to be done once. All other iterations are done parallel
25+
helper(pieces[0])
26+
pieces = pieces[1:]
27+
if pieces: tasks.Parallel.ForEach(pieces, helper)
28+
if flatten_results:
29+
temp = []
30+
for result in results:
31+
if type(result) is list:
32+
for r in result: temp.append(r)
33+
else:
34+
temp.append(result)
35+
results = temp
36+
return results

0 commit comments

Comments
 (0)