Closest Point script parallel

I am trying to run Closest Point component in parallel since it runs as many times as Points you input. It Keeps giving me an error (“Data conversion failed from Number to Point”) when trying to run it in parallel but it works fine when using it without it.

import rhinoscriptsyntax as rs
import ghpythonlib.components as gh
import ghpythonlib.parallel

def cls_point(pt):
    new_pt = []
    for i in range(len(pt)):
        temp = gh.ClosestPoint(pt[i], pts)[0]
        new_pt.append(temp)
    return(new_pt)

if parallel:
    a = ghpythonlib.parallel.run(cls_point, pt, True)
else:
    a = cls_point(pt)

Thanks

Closest Point Parallel_Test.gh (8.6 KB)

The documentation string from ghpythonlib.parallel.run says:

for each item in data_list execute the input function. Execution is
    done on as many threads as there are CPUs on the computer.
    Parameters:
        function: function to execute for each item in the data_list
        data_list: list, tuple, or other enumerable data structure
        flatten_list [opt]: if True, when results are lists of lists the
          results are flattened into a single list of results. If this is True,
          you cannot depend on the result list being the same size as the input list
    Returns:
        list of results containing the return from each call to the input function

So it is enumerating the list you pass in the data_list parameter, running your function on each item and returning the combined results of each call in a list.

If you change your code to this, it will work as you are expecting:

import rhinoscriptsyntax as rs
import ghpythonlib.components as gh
import ghpythonlib.parallel

def cls_point(pt):
    if parallel:
        return gh.ClosestPoint(pt, pts)[0]
    new_pt = []
    for i in range(len(pt)):
        temp = gh.ClosestPoint(pt[i], pts)[0]
        new_pt.append(temp)
    return(new_pt)

if parallel:
    a = ghpythonlib.parallel.run(cls_point, pt, True)
else:
    a = cls_point(pt)

Closest Point Parallel_Test_211028a.gh (6.8 KB)

-Kevin

Thanks @kev.r

I am still learning so I appreciate the feedback :slightly_smiling_face: