| 1 | n/a | from multiprocessing import freeze_support |
|---|
| 2 | n/a | from multiprocessing.managers import BaseManager, BaseProxy |
|---|
| 3 | n/a | import operator |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | ## |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | class Foo: |
|---|
| 8 | n/a | def f(self): |
|---|
| 9 | n/a | print('you called Foo.f()') |
|---|
| 10 | n/a | def g(self): |
|---|
| 11 | n/a | print('you called Foo.g()') |
|---|
| 12 | n/a | def _h(self): |
|---|
| 13 | n/a | print('you called Foo._h()') |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | # A simple generator function |
|---|
| 16 | n/a | def baz(): |
|---|
| 17 | n/a | for i in range(10): |
|---|
| 18 | n/a | yield i*i |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | # Proxy type for generator objects |
|---|
| 21 | n/a | class GeneratorProxy(BaseProxy): |
|---|
| 22 | n/a | _exposed_ = ['__next__'] |
|---|
| 23 | n/a | def __iter__(self): |
|---|
| 24 | n/a | return self |
|---|
| 25 | n/a | def __next__(self): |
|---|
| 26 | n/a | return self._callmethod('__next__') |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | # Function to return the operator module |
|---|
| 29 | n/a | def get_operator_module(): |
|---|
| 30 | n/a | return operator |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | ## |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | class MyManager(BaseManager): |
|---|
| 35 | n/a | pass |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | # register the Foo class; make `f()` and `g()` accessible via proxy |
|---|
| 38 | n/a | MyManager.register('Foo1', Foo) |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | # register the Foo class; make `g()` and `_h()` accessible via proxy |
|---|
| 41 | n/a | MyManager.register('Foo2', Foo, exposed=('g', '_h')) |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | # register the generator function baz; use `GeneratorProxy` to make proxies |
|---|
| 44 | n/a | MyManager.register('baz', baz, proxytype=GeneratorProxy) |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | # register get_operator_module(); make public functions accessible via proxy |
|---|
| 47 | n/a | MyManager.register('operator', get_operator_module) |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | ## |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | def test(): |
|---|
| 52 | n/a | manager = MyManager() |
|---|
| 53 | n/a | manager.start() |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | print('-' * 20) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | f1 = manager.Foo1() |
|---|
| 58 | n/a | f1.f() |
|---|
| 59 | n/a | f1.g() |
|---|
| 60 | n/a | assert not hasattr(f1, '_h') |
|---|
| 61 | n/a | assert sorted(f1._exposed_) == sorted(['f', 'g']) |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | print('-' * 20) |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | f2 = manager.Foo2() |
|---|
| 66 | n/a | f2.g() |
|---|
| 67 | n/a | f2._h() |
|---|
| 68 | n/a | assert not hasattr(f2, 'f') |
|---|
| 69 | n/a | assert sorted(f2._exposed_) == sorted(['g', '_h']) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | print('-' * 20) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | it = manager.baz() |
|---|
| 74 | n/a | for i in it: |
|---|
| 75 | n/a | print('<%d>' % i, end=' ') |
|---|
| 76 | n/a | print() |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | print('-' * 20) |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | op = manager.operator() |
|---|
| 81 | n/a | print('op.add(23, 45) =', op.add(23, 45)) |
|---|
| 82 | n/a | print('op.pow(2, 94) =', op.pow(2, 94)) |
|---|
| 83 | n/a | print('op._exposed_ =', op._exposed_) |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | ## |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | if __name__ == '__main__': |
|---|
| 88 | n/a | freeze_support() |
|---|
| 89 | n/a | test() |
|---|