Я dill
автор.dill
может передавать метод класса на другой компьютер, как показано ниже.
>$ python
Python 3.5.6 (default, Sep 20 2018, 12:15:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
... def bar(self, x):
... return self.y + x
... def __init__(self, y):
... self.y = y
...
>>> import dill
>>>
>>> f = Foo(5)
>>>
>>> with open('foo.pkl', 'wb') as pkl:
... dill.dump(f.bar, pkl)
...
>>>
Затем в новом сеансе (или на другом компьютере) ...
>$ python
Python 3.5.6 (default, Sep 20 2018, 12:15:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> with open('foo.pkl', 'rb') as pkl:
... b = dill.load(pkl)
...
>>> b(4)
9
Без вашего более конкретного кода трудно сказать, почему вы не видите такого поведения ... но dill
предоставляет возможность передавать определение класса (или просто метод класса) на другой компьютер.
Это то, что позволяет коду, подобному pathos
, передавать метод класса на другой компьютер в ParallelPool
или ProcessPool
- последний относится к процессам, а первый - к распределенным ресурсам.
dude>$ python
Python 3.5.6 (default, Sep 20 2018, 12:15:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> class Foo(object):
... def bar(self, x):
... return self.y + x
... def __init__(self, y):
... self.y = y
...
>>> import pathos
>>> p = pathos.pools.ParallelPool()
>>> p.map(Foo(4).bar, [1,2,3])
[5, 6, 7]
>>> p.close(); p.join()
>>>