Shared variable in Python Process subclass
Question: I was wondering if it would be possible to create some sort of static set in a
Python Process subclass to keep track the types processes that are currently
running asynchronously.

    
    
    class showError(Process):
        # Define some form of shared set that is shared by all Processes
        displayed_errors = set()
    
        def __init__(self, file_name, error_type):
            super(showError, self).__init__()
            self.error_type = error_type
    
        def run(self):
            if error_type not in set:
                displayed_errors.add(error_type)
                message = 'Please try again. ' + str(self.error_type)
                winsound.MessageBeep(-1)
                result = win32api.MessageBox(0, message, 'Error', 0x00001000)
    
                if result == 0:
                    displayed_errors.discard(error_type)
    

That way, when I create/start multiple showError processes with the same
error_type, subsequent error windows will not be created. So how can we define
this shared set?


Answer: You can use a
[`multiprocessing.Manager.dict`](https://docs.python.org/2/library/multiprocessing.html#multiprocessing.managers.SyncManager.dict)
(there's no `set` object available, but you can use a `dict` in the same way)
and share that between all your subprocesses.

    
    
    import multiprocessing as mp
    
    if __name__ == "__main__":
        m = mp.Manager()
        displayed_errors = m.dict()
        subp = showError("some filename", "some error type", displayed_errors)
    

Then change `showError.__init__` to accept the shared dict:

    
    
    def __init__(self, file_name, error_type, displayed_errors):
        super(showError, self).__init__()
        self.error_type = error_type
        self.displayed_errors = displayed_errors
    

Then this:

    
    
    displayed_errors.add(error_type)
    

Becomes:

    
    
    self.displayed_errors[error_type] = 1
    

And this:

    
    
    displayed_errors.discard(error_type)
    

Becomes:

    
    
    try:
        del self.displayed_errors[error_type]
    except KeyError:
        pass
    

