1

I am trying to get a result from the tasks method in IronPython similar to how results.get works with CPython multiprocessing.Queue

from System.Threading.Tasks import *
def testThread(dataPnt,constData):
    return dataPnt

def main():

    dataToSplit = range(5)
    constData = 10

    threadResult = Parallel.ForEach(dataToSplit, lambda dataPnt: testThread(dataPnt,constData))

    print(threadResult)

main()

At this point threadResult is System.Threading.Tasks.ParallelLoopResult but I can't find any decent documentation. The closest has been this post but it is incrementing and I need an array returned.

user-2147482637
  • 2,115
  • 8
  • 35
  • 56

1 Answers1

1

Parallel.ForEach is a multi-processing operation but given that you expect a result from your operation you would either have to use a shared result variable (e.g. some sort of concurrent collection) or use a parallel LINQ projection operation.

An approach close to your sample could look like:

# get LINQ dependencies
import clr
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

from System.Threading.Tasks import *
def testThread(dataPnt,constData):
    print dataPnt
    return dataPnt

def main():

    dataToSplit = range(5)
    constData = 10

    threadResult = dataToSplit.AsParallel().Select(lambda dataPnt: testThread(dataPnt,constData)).ToList()

    print(threadResult)

main()
Simon Opelt
  • 6,136
  • 2
  • 35
  • 66