Hello guys,
I’m trying to compare the difference in parallel computing between ghpythonlib and System.Threading.Tasks.
However, I encountered a problem that the Tasks method didn’t give me a correct result (sequences are messed up). And I assume it’s because I wrote the function in the wrong way.
There is my code:
import rhinoscriptsyntax as rs
from System.Threading import Tasks
list = []
def math(y):
value = y+1
list.append(value)
Tasks.Parallel.ForEach(x,math)
a = list
If anyone can help me to take a look, I’ll be really appreciated
Thank you!
ParallelTest2.gh (11.0 KB)
ghpythonlib.parallel
is a wrapper around System.Threading.Tasks.Parallel.ForEach
. You can inspect the source code here:
C:\Users\USERNAME\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\ghpythonlib\parallel.py
Hello,
Thanks for your reply!
I am curious about how to achieve the correct result (correct sequences) using the “System.Threading.Tasks.Parallel.ForEach” method, or how should I modify my code?
There are probably several ways of maintaining the input order. The method I usually go with is to input a list of integers, and then replace these with whatever the output is, as one would with an index-based loop. This has the added benefit that it is simply to switch between threaded and non-threaded without rewriting the task function. Here’s a recent real world example of mine demonstrating this pattern: