Skip to content

Instantly share code, notes, and snippets.

@andyfaff
Last active May 23, 2024 07:07
Show Gist options
  • Save andyfaff/9cdb090af66474a53a2f17381fa7aa93 to your computer and use it in GitHub Desktop.
Save andyfaff/9cdb090af66474a53a2f17381fa7aa93 to your computer and use it in GitHub Desktop.
Pickle-free multiprocessing
import stuff
import multiprocessing
import pickle
from refnx._lib.emcee import EnsembleSampler
nvp = len(objective.varying_parameters())
with multiprocessing.pool.Pool(processes=None, initializer=stuff.initialiser, initargs=(pickle.dumps(objective),)) as pwl:
sampler = EnsembleSampler(200, nvp, stuff.calculator, pool=pwl)
%time sampler.run_mcmc(fitter.chain[-1], 4000)
import pickle
obj = None
def initialiser(*args):
global obj
obj = pickle.loads(args[0])
def calculator(x):
return obj.logpost(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment