Pickle cython class in Python Multiprocessing

I have a class that is implemented in cython containing c pointers that I am trying to use with the python multiprocessing module. The class returns a DLL file for returning an instance of the class.

The problem is that while the instances keep their data type, they seem empty, that is, I can access all of their class functions, but they lost all their instance values ​​that I installed before they were entered. The code containing special_class is very large, so I cannot include it.

import time
import multiprocessing as mp
from special_module import special_class

def run_task(tasks,nr):
    obj = tasks[nr]['data']
    print obj.get_name()



if __name__ == "__main__":

    m1 = special_class("a.dll")
    m2 = special_class("b.dll")


    tasks = dict()

    tasks[1] = {'data': m1}
    tasks[2] = {'data': m2}


    process1 = mp.Process(target = run_task, name = 'process1', args = (tasks, 1))
    process2 = mp.Process(target = run_task, name = 'process2', args = (tasks, 2))

    process1.start()

    time.sleep(0.2)

    process2.start()

    process1.join()
    process2.join()

The above script gives me the output

None
None

The correct conclusion should be in style

name.a
name.b

If I create instances inside the run_task function, it will work fine, but I'm looking for a way to make it work by creating instances in the main process. Is it possible?

+4
3

, . , special_class .

run_task,

, , .


special_class picklable. -. : https://docs.python.org/3/library/pickle.html#pickle-inst

, 3 :

  • __reduce__ special_class
  • __getstate__ __setstate__ special_class ( )

, special_class. : https://docs.python.org/3/library/pickle.html#persistence-of-external-objects

0

, . Python, special_class. special_class.__ reduce__, .

+2

, m1 m2 special_class . , :

from special_class import *

( )

m1 = special_class.special_class("a.dll")
m2 = special_class.special_class("b.dll")

, None, , m1 m2, - . from special_class import * .

0
source

Source: https://habr.com/ru/post/1662124/


All Articles