Python Forum
Python 3.5 Instantiate and manipulate object with multiprocessing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.5 Instantiate and manipulate object with multiprocessing
#1
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)
Reply
#2
Have you taken a look at this? https://docs.python.org/3/library/multip...-processes
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is this a multiprocessing bug in Python - or am I doing something wrong? haimat 1 1,185 Oct-18-2023, 06:07 AM
Last Post: absalom1
  How do I instantiate a class with **kwargs? palladium 6 7,370 Feb-16-2023, 06:10 PM
Last Post: deanhystad
  python multiprocessing help -- to extract 10 sql table into csv mg24 3 1,392 Nov-20-2022, 11:50 PM
Last Post: mg24
  Openpyxl manipulate excel write formula SamLiu 0 1,056 Nov-04-2022, 03:00 PM
Last Post: SamLiu
  python multiprocessing to download sql table mg24 5 1,476 Oct-31-2022, 03:53 PM
Last Post: Larz60+
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,046 Sep-19-2022, 02:32 AM
Last Post: Xeno
  python update binary object (override delivered Object properties) pierre38 4 1,768 May-19-2022, 07:52 AM
Last Post: pierre38
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 6,427 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  Multiprocessing Can't pickle local object law 1 16,021 Aug-30-2021, 02:49 PM
Last Post: law
  multiprocessing and sharing object cyrduf 0 2,051 Feb-02-2021, 08:16 PM
Last Post: cyrduf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020