2 # This module shows how to use arbitrary callables with a subclass of
5 # Copyright (c) 2006-2008, R Oudkerk
9 from multiprocessing
import freeze_support
10 from multiprocessing
.managers
import BaseManager
, BaseProxy
17 print 'you called Foo.f()'
19 print 'you called Foo.g()'
21 print 'you called Foo._h()'
23 # A simple generator function
28 # Proxy type for generator objects
29 class GeneratorProxy(BaseProxy
):
30 _exposed_
= ('next', '__next__')
34 return self
._callmethod
('next')
36 return self
._callmethod
('__next__')
38 # Function to return the operator module
39 def get_operator_module():
44 class MyManager(BaseManager
):
47 # register the Foo class; make `f()` and `g()` accessible via proxy
48 MyManager
.register('Foo1', Foo
)
50 # register the Foo class; make `g()` and `_h()` accessible via proxy
51 MyManager
.register('Foo2', Foo
, exposed
=('g', '_h'))
53 # register the generator function baz; use `GeneratorProxy` to make proxies
54 MyManager
.register('baz', baz
, proxytype
=GeneratorProxy
)
56 # register get_operator_module(); make public functions accessible via proxy
57 MyManager
.register('operator', get_operator_module
)
70 assert not hasattr(f1
, '_h')
71 assert sorted(f1
._exposed_
) == sorted(['f', 'g'])
78 assert not hasattr(f2
, 'f')
79 assert sorted(f2
._exposed_
) == sorted(['g', '_h'])
90 op
= manager
.operator()
91 print 'op.add(23, 45) =', op
.add(23, 45)
92 print 'op.pow(2, 94) =', op
.pow(2, 94)
93 print 'op.getslice(range(10), 2, 6) =', op
.getslice(range(10), 2, 6)
94 print 'op.repeat(range(5), 3) =', op
.repeat(range(5), 3)
95 print 'op._exposed_ =', op
._exposed
_
99 if __name__
== '__main__':