For lighter workloads where GIL contention is not a concern, ThreadPoolExecutor can be used:
from concurrent.futures import ThreadPoolExecutor, as_completedimport dynexfrom dynex import DynexConfig, ComputeBackend, DynexSampler, BQMdef run_job(args): job_id, bqm = args config = DynexConfig(compute_backend=ComputeBackend.GPU) model = BQM(bqm) sampler = DynexSampler(model, config=config, logging=False) sampleset = sampler.sample(num_reads=500, annealing_time=100) return job_id, sampleset.first.energybqms = [build_bqm(i) for i in range(4)] # Your model-building functionwith ThreadPoolExecutor(max_workers=4) as executor: futures = {executor.submit(run_job, (i, bqm)): i for i, bqm in enumerate(bqms)} for future in as_completed(futures): job_id, energy = future.result() print(f"Job {job_id} best energy: {energy:.4f}")
Use multiprocessing.Process (not threads) for CPU-intensive sampling. Python’s GIL prevents true parallelism with threads for compute-heavy workloads.