Python Forum
Python 3.5 Instantiate and manipulate object with multiprocessing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python 3.5 Instantiate and manipulate object with multiprocessing (/thread-1336.html)



Python 3.5 Instantiate and manipulate object with multiprocessing - jipete - Dec-25-2016

Hello, I am looking for a solution to be able to instantiate an object that will exploit another heart of cpu all by manipulating it from another process.
Currently the only solution I found is to use pickle.
But :
1) But I find this solution low performing
2) I can not for example create a tkinter window in my object

This is my code, Would you have more optimal solutions to offer me ?

 
import multiprocessing as m
import pickle
 
class Store:
    pass
 
class Shareable:
    def __init__(self, size = 2**10):
        object.__setattr__(self, 'store', m.Array('B', size))
        o = Store() # This object will hold all shared values
        s = pickle.dumps(o)
        store(object.__getattribute__(self, 'store'), s)
 
    def __getattr__(self, name):
        s = load(object.__getattribute__(self, 'store'))
        o = pickle.loads(s)
        return getattr(o, name)
 
    def __setattr__(self, name, value):
        s = load(object.__getattribute__(self, 'store'))
        o = pickle.loads(s)
        setattr(o, name, value)
        s = pickle.dumps(o)
        store(object.__getattribute__(self, 'store'), s)
 
def store(arr, s):
    for i, ch in enumerate(s):
        arr[i] = ch
 
def load(arr):
    l = arr[:]
    return bytes(arr)
 
class Foo(Shareable):
    def __init__(self):
        super().__init__()
        self.f = 1
        self.f2 = 1
        self.mylist=[0,1,2,3,4,5,6]
 
    def foo(self):
        self.f += 1
        """while True:
            self.f2+=1"""
 
def Otherprocess(s):
    print("Process2 :")
    print(s.f)
    s.mylist=[0]
    """while True:
        s.f2+=1"""
 
if __name__ == '__main__':
    import multiprocessing as m
    import time
    s = Foo()
    print(s.f)
    p = m.Process(target=s.foo, args=())
    p.start()
    #p.join()
    p2 = m.Process(target=Otherprocess, args=(s,))
    p2.start()
    time.sleep(1)
    print("Process1 :")
    print(s.f)
    print(s.mylist)



RE: Python 3.5 Instantiate and manipulate object with multiprocessing - micseydel - Dec-28-2016

Have you taken a look at this? https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes