Berkay Akyazı
i'm Berkay

i'm Berkay

Python Singleton classes & Multiprocessing

Berkay Akyazı's photo
Berkay Akyazı
·Aug 17, 2021·

1 min read

Python Singleton classes & Multiprocessing

I'm sure many of you already know that but I want to write about my today challenge. In my python project, I have Singleton classes which are created by this metaclass

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super().__call__(*args, **kwargs)
        return cls._instances[cls]

class SingletonClass(metaclass=Singleton):
    def __init__(self):
        ## do something initialize it
        pass

Also, at runtime I create sub-processes with built-in multiprocessing library, and these sub-processes use this SingletonClass. I don't know maybe I'm too naïve, but I was expecting that every sub-process would initialize this class once. However, it didn't happen, because I had already initialized it in main process before I created sub-processes. So, sub-processes could not initialize it again, but it is important to me because I do something critical in constructor for the sub-process.

I had not known this issue until I faced it. I was able to solve my problem by removing construction calls in main thread, but sometimes it may not be possible. Creating sub-processes before, or re-write this class for sub-process may be alternative solutions.

What do you think? Is there any other option? And I will be so happy if someone explain why this happens?

Thanks..

 
Share this