Multiprocessing in Python Data Function

Hello,

trying to run a Python data function that uses multiprocessing results in the error "_pickle.PicklingError: Can't pickle <class 'Worker'>: attribute lookup Worker on builtins failed". When running the same script outside of Spotfire with the local Python interpreter, everything works fine. Does anyone have any idea what could be the cause for the different behavior of the script?

Thanks Uwe

import multiprocessing as mp
import time
import random as rd

class Task(object):
    def __init__(self, task:int) -> None:
        self.__task = task

    def __call__(self, worker:int) -> float:
        t = rd.random()
        time.sleep(t)
        return(t)

class Worker(mp.Process):
    def __init__(self, tasks:mp.JoinableQueue, results:mp.Queue) -> None:
        mp.Process.__init__(self)
        self.__tasks   = tasks
        self.__results = results

    def run(self) -> None:
        number  = int(self.name.split('-')[1])
        while True:
            task = self.__tasks.get()
            if task is None:
                self.__tasks.task_done()
                break
            answer = task(number)
            self.__tasks.task_done()
            self.__results.put(answer)


        return

class Master(object):
    def run(self, numTasks:int, numWorker:int) -> None:
        tasks = mp.JoinableQueue()
        for i in range(numTasks):
            tasks.put(Task(i))
        for i in range(numWorker):
            tasks.put(None)
        results = mp.Queue()
        for i in range(numWorker):
            c = Worker(tasks, results)
            c.start()
        tasks.join()
        return(sum([results.get() for i in range(results.qsize())])/numTasks)


rd.seed(time.time())
m = Master().run(10, 3)

 

(2) Answers

Login