# Ghpython and parallel

**URL:** <https://discourse.mcneel.com/t/ghpython-and-parallel/125510>\
**Category:** Grasshopper Developer\
**Tags:** windows\
**Created:** [June 7, 2021, 10:24am UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510 "2021-06-07T10:24:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [June 7, 2021, 10:24am UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/1 "2021-06-07T10:24:51Z")

</div>

Hello , is parallel enabled by default in Ghpython ?  
If not, how to use it with python script?

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [June 7, 2021, 11:01am UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/2 "2021-06-07T11:01:26Z")

</div>

Have you checked this blog post?

> **[ghPython – New component and parallel modules](https://stevebaer.wordpress.com/2013/12/11/ghpython-node-in-code/)**
>
> Just in time for Christmas… ghPython 0.6.0.3 was released this week and it has two new features that I’m really excited about. Download ghPython… A little background David Rutten …

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [June 7, 2021, 11:26am UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/3 "2021-06-07T11:26:12Z")

</div>

Yes but don’t work when the function have many input and it looks work only with `ghpythonlib.components`

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/9/c/5/9c5d65159377c7ff3b58f1304024712d5ae91958.png)

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [June 7, 2021, 11:49am UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/4 "2021-06-07T11:49:51Z")

</div>

![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/f/7/0/f70fc4b1bdf02249145858f58b29d28c0265b563.png)

```python
import Rhino.Geometry as rg
from ghpythonlib.parallel import run
def arc(args):
    center, radius, angle = args
    c = rg.Circle(center, radius)
    return rg.Arc(c, angle)
a = run(arc, zip(centers, radiuses, angles), True)

```

[Seghier.gh](https://discourse.mcneel.com/uploads/short-url/cOHDVGvDLLJXRuINs1ZEonNF7ng.gh) (5.9 KB)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [June 7, 2021, 12:18pm UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/5 "2021-06-07T12:18:19Z")

</div>

Thank you @Mahdiyar  
So the inputs must have list access? i did similar thing with item access but i got duplicate result 3 times;  
and for a complex function it is not easy to use parallel

---

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [June 7, 2021, 3:37pm UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/6 "2021-06-07T15:37:16Z")

</div>

> [@anon39580149](#):
>
> and for a complex function it is not easy to use parallel

That’s exactly the problem – writing parallel code that actually works is very complicated. Sometimes it’s easier to write single-threaded code. The time to debug outweighs the time to execute.

---

<div class="post-metadata">

**Author:** ![TomTom](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/tomtom/32/651939_2.png) [@TomTom](https://discourse.mcneel.com/u/TomTom)\
**Post date:** [June 8, 2021, 5:46am UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/7 "2021-06-08T05:46:05Z")

</div>

There is also the Parallel.For & Parallel.ForEach part of the System.Threading.Tasks namespace. You just provide a lambda function…

I usually do this if it really needs to be fast:

Write Code as readable as possible → Measure performance → Identify the bottleneck → Put the critical code in one function → Find a better single-threaded solution by improving the algorithm (Solves it for 90%) → Rewrite generalized library functionality (e.g. from Rhinocommon) to only do the things required (Solves it for 99%) → If it can be multi-threaded, implement parallel solution → Try to apply any sort of ‘micro’-optimisations (no safety checks, ‘branchless’, directly access of memory, SIMD) → Rewrite portions in C and/or Assembly.

But I only had 2 situations where any of these steps were required. None of them where part of Rhino/GH. It just doesn’t make sense to improve to perfection, just to gain 200 ms.

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [June 8, 2021, 6:54am UTC](https://discourse.mcneel.com/t/ghpython-and-parallel/125510/8 "2021-06-08T06:54:06Z")

</div>

> [@TomTom](#):
>
> There is also the Parallel.For & Parallel.ForEach part of the System.Threading.Tasks namespace.

The `ghpythonlib.parallel.run` function is actually just a friendly wrapper around `System.Threading.Tasks.Parallel.ForEach`.
