#!/usr/bin/env python
"""Single robust launcher: spawn N download_continuum workers as subprocesses,
wait for all. Launch this once with nohup."""
import subprocess, sys, os, time
N=8
here=os.path.dirname(os.path.abspath(__file__))
log=open(os.path.join(here,'run_parallel.log'),'a')
def p(*a):
    s=time.strftime('%H:%M:%S ')+' '.join(str(x) for x in a); print(s,file=log); log.flush()
procs=[]
for w in range(N):
    pr=subprocess.Popen([sys.executable,os.path.join(here,'download_continuum.py'),str(w),str(N)],
                        cwd=here, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    procs.append(pr); p(f'spawned worker {w}/{N} pid={pr.pid}'); time.sleep(1)
p(f'all {N} workers spawned; waiting')
for pr in procs: pr.wait()
p('all workers finished')
log.close()
