Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Running a singleton with multiprocessing in python throws error.
import time
from joblib import Parallel, delayed
class OnlyOne:
"""Singleton Class, inspired by"""
class __OnlyOne:
def __init__(self, arg):
if arg is None:
raise ValueError("Pretend empty instantiation breaks code")
self.val = arg
def __str__(self):
return repr(self) + self.val
instance = None
def __init__(self, arg=None):
if not self.instance:
self.instance = self.__OnlyOne(arg)
self.instance.val = arg
def __getattr__(self, name):
return getattr(self.instance, name)
def worker(num):
"""Single worker function to run in parallel.
Assume that this function has to do an empty
instantiation of the singleton.
one = OnlyOne()
one.val += num
return one.val
# Instantiate singleton
one = OnlyOne(0)
# Try to run in parallel
# Will hit the ValueError that raises with
# empty instantiation
res = Parallel(n_jobs=-1, verbose=10)(
delayed(worker)(i) for i in range(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment