Search code examples
python-3.xpython-multiprocessingcode-reuse

Is there way to write code usable for both of the multi-processing and single-process in python?


The problem is the lock. The multiprocessing needs a lock but not for the single process. For example, consider the following code:

Class Test():
    
    def __init__(self, rlock = None):
        self.tlock = rlock

    def do_test(self, invalue):
        with self.tlock:
            return invalue + 1

For the multiprocessing, I need to use the tlock but when I use the class for single process, I don't need it. So the line with self.tlock doesn't make sense for a single process execution.

My immediate thought is to write in the following way:

def do_test(self, invalue):
    if self.tlock is not None:
        with self.tlock:
           return invalue + 1
    else:
        return invalue + 1

But this looks so awkward as I would have handful of methods of this pattern inside the class.

Is there any elegant and efficient way to write the code for the code reuse?


Solution

  • You can create a dummy class that can support context managers, and use that instead of storing the lock if it is None (i.e, no multiprocessing is involved):

    class DummyLock:
        def __enter__(self):
            pass
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            pass
    
    
    class Test:
    
        def __init__(self, rlock=None):
            
            self.tlock = rlock
            if self.tlock is None:
                self.tlock = DummyLock()
    

    All other methods in the class will not need to be changed unless they are accessing self.rlock specific methods (like self.rlock.acquire()) instead of using context managers (with self.rlock:).