"""Parallel souz wave: N worker threads, each registers its own TG session.""" import importlib.util, json, os, queue, sys, threading, time HERE = os.environ.get("SOUZ_HOME", os.environ.get("SOUZ_HOME", os.path.dirname(os.path.abspath(__file__)))) spec = importlib.util.spec_from_file_location("sw", os.path.join(HERE, "souz_wave.py")) sw = importlib.util.module_from_spec(spec) spec.loader.exec_module(sw) BANK = os.environ.get("SOUZ_BANK", os.path.join(HERE, "souz_bank.jsonl")) LOCK = threading.Lock() def tried(): out = set() if os.path.exists(BANK): for line in open(BANK, encoding="utf-8"): try: out.add(json.loads(line).get("session")) except Exception: pass return out def main(): limit = int(sys.argv[1]) if len(sys.argv) > 1 else 100 workers = int(sys.argv[2]) if len(sys.argv) > 2 else 6 done = tried() all_s = sw.load_sessions() todo = [s for s in all_s if s not in done][:limit] q = queue.Queue() for s in todo: q.put(s) print("todo=%d workers=%d" % (len(todo), workers), flush=True) counters = {"ok": 0, "fail": 0, "bonus": 0} def worker(idx): while True: try: sp = q.get_nowait() except queue.Empty: return t0 = time.time() try: res = sw.register(sp) except Exception as e: res = {"session": sp, "err": "exc:%s" % type(e).__name__} res["elapsed"] = round(time.time() - t0, 1) with LOCK: with open(BANK, "a", encoding="utf-8") as f: f.write(json.dumps(res, ensure_ascii=False) + "\n") if res.get("ok"): counters["ok"] += 1 bal = (res.get("balance") or {}).get("available_micro") or 0 if bal > 0: counters["bonus"] += 1 print("OK b=%s %s %.0fs [ok=%d bonus=%d]" % (bal, os.path.basename(sp)[:26], res["elapsed"], counters["ok"], counters["bonus"]), flush=True) else: counters["fail"] += 1 print("FAIL %s %s" % (os.path.basename(sp)[:26], res.get("err")), flush=True) ths = [threading.Thread(target=worker, args=(i,), daemon=True) for i in range(workers)] for t in ths: t.start() for t in ths: t.join() print("DONE ok=%d fail=%d bonus=%d" % (counters["ok"], counters["fail"], counters["bonus"]), flush=True) if __name__ == "__main__": main()