[ create a new paste ] login | about

Link: http://codepad.org/zW2LU6XV    [ raw code | output | fork ]

Python, pasted on Mar 5:
import multiprocessing.managers as m
import multiprocessing

class MySharedClass:

    def __init__(self):
        self.a = 0

    def inc(self):
        self.a += 1

    def geta(self):
        return self.a

def f(a):
    a.inc()

def g(f):
    f.write('1')


class MyManager(m.BaseManager):
    pass

MyManager.register("MySharedClass", MySharedClass)
MyManager.register("open", open)



if __name__ == '__main__':
    manager = MyManager()
    manager.start()
    a = manager.MySharedClass()

    n = 100000
    p = multiprocessing.Pool(10)
    p.map(f, [a] * n)
    print('{} == {}'.format(a.geta(), n)) # 99996 == 100000

    file = manager.open('test.txt', 'w')
    
    p.map(g, [file] * n)

    print('{} == {}'.format(len(open('test.txt').read()), n)) # 98316 == 100000


Output:
1
2
3
4
Traceback (most recent call last):
  Line 1, in <module>
    import multiprocessing.managers as m
ImportError: No module named multiprocessing.managers


Create a new paste based on this one


Comments: