Guest User

Untitled

a guest
Nov 21st, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. import multiprocessing, random, time
  2.  
  3. def multiprocessing_patch(self):
  4.    
  5.     # Including this function inside a class definition causes a recursion depth
  6.     # exceeded error.
  7.    
  8.     key = object.__getattribute__(self, '_local__key')
  9.     d = multiprocessing.current_process().__dict__.get(key)
  10.     if d is None:
  11.         d = {}
  12.         multiprocessing.current_process().__dict__[key] = d
  13.         object.__setattr__(self, '__dict__', d)
  14.  
  15.         # we have a new instance dict, so call out __init__ if we have
  16.         # one
  17.         cls = type(self)
  18.         if cls.__init__ is not object.__init__:
  19.             args, kw = object.__getattribute__(self, '_local__args')
  20.             cls.__init__(self, *args, **kw)
  21.     else:
  22.         object.__setattr__(self, '__dict__', d)
  23.    
  24. class MultiprocessingLocal(object):
  25.    
  26.     __slots__ = '_local__key', '_local__args', '_local__lock'
  27.  
  28.     def __new__(cls, *args, **kw):
  29.         self = object.__new__(cls)
  30.         key = '_local__key', 'multiprocessing_local.' + str(id(self))
  31.         object.__setattr__(self, '_local__key', key)
  32.         object.__setattr__(self, '_local__args', (args, kw))
  33.         object.__setattr__(self, '_local__lock', multiprocessing.RLock())
  34.  
  35.         if args or kw and (cls.__init__ is object.__init__):
  36.             raise TypeError("Initialization arguments are not supported")
  37.  
  38.         # We need to create the thread dict in anticipation of
  39.         # __init__ being called, to make sure we don't call it
  40.         # again ourselves.
  41.         dict_ = object.__getattribute__(self, '__dict__')
  42.         multiprocessing.current_process().__dict__[key] = dict_
  43.  
  44.         return self
  45.  
  46.     def __getattribute__(self, name):
  47.         lock = object.__getattribute__(self, '_local__lock')
  48.         lock.acquire()
  49.         try:
  50.             multiprocessing_patch(self)
  51.             return object.__getattribute__(self, name)
  52.         finally:
  53.             lock.release()
  54.  
  55.     def __setattr__(self, name, value):
  56.         lock = object.__getattribute__(self, '_local__lock')
  57.         lock.acquire()
  58.         try:
  59.             multiprocessing_patch(self)
  60.             return object.__setattr__(self, name, value)
  61.         finally:
  62.             lock.release()
  63.  
  64.     def __delattr__(self, name):
  65.         lock = object.__getattribute__(self, '_local__lock')
  66.         lock.acquire()
  67.         try:
  68.             multiprocessing_patch(self)
  69.             return object.__delattr__(self, name)
  70.         finally:
  71.             lock.release()
  72.  
  73. class Tester(MultiprocessingLocal):
  74.    
  75.     def __init__(self):
  76.         self.pid = multiprocessing.current_process().ident
  77.         self.random = random.random()
  78.        
  79.     def print_state(self):
  80.         time.sleep(random.random()/10)
  81.         print(self.pid, self.random)
  82.  
  83. tester = Tester()
  84. tester.print_state()
  85. for _ in range(4):
  86.     multiprocessing.Process(target=tester.print_state).start()
Add Comment
Please, Sign In to add comment