{"record":{"id":"ec1fa12e20b9c973","repo":"microsoft/qlib","slug":"need-to-call-activate-to-launch-a-daemon-worker","errorCode":null,"errorMessage":"Need to call activate() to launch a daemon worker to produce data into data queue before using it. You probably have forgotten to use the DataQueue in a with block.","messagePattern":"Need to call activate\\(\\) to launch a daemon worker to produce data into data queue before using it\\. You probably have forgotten to use the DataQueue in a with block\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/rl/utils/data_queue.py","lineNumber":152,"sourceCode":"\n    def done(self) -> int:\n        return self._done.value\n\n    def activate(self) -> DataQueue:\n        if self._activated:\n            raise ValueError(\"DataQueue can not activate twice.\")\n        thread = threading.Thread(target=self._producer, daemon=True)\n        thread.start()\n        self._activated = True\n        return self\n\n    def __del__(self) -> None:\n        _logger.debug(f\"__del__ of {__name__}.DataQueue\")\n        self.cleanup()\n\n    def __iter__(self) -> Generator[Any, None, None]:\n        if not self._activated:\n            raise ValueError(\n                \"Need to call activate() to launch a daemon worker \"\n                \"to produce data into data queue before using it. \"\n                \"You probably have forgotten to use the DataQueue in a with block.\",\n            )\n        return self._consumer()\n\n    def _consumer(self) -> Generator[Any, None, None]:\n        while True:\n            try:\n                yield self.get()\n            except StopIteration:\n                _logger.debug(\"Data consumer timed-out from get.\")\n                return\n\n    def _producer(self) -> None:\n        # pytorch dataloader is used here only because we need its sampler and multi-processing\n        from torch.utils.data import DataLoader, Dataset  # pylint: disable=import-outside-toplevel\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/rl/utils/data_queue.py#L134-L170","documentation":"ValueError from `DataQueue.__iter__` (qlib/rl/utils/data_queue.py:152). Iterating a DataQueue before its daemon producer thread was started is forbidden; activation happens in `__enter__`, so this almost always means the queue was used outside its `with` block.","triggerScenarios":"`for x in DataQueue(generator):` without a surrounding `with`, or iterating a queue created earlier whose with-block has already exited; passing an unactivated DataQueue to code that iterates it.","commonSituations":"Refactoring training code and losing the with-statement; helper functions receiving the queue and iterating it while the caller forgot to enter the context; notebooks where cells run out of order relative to the with-block.","solutions":["Wrap usage in the context manager: `with DataQueue(producer) as q: consume(q)`.","If you must manage it manually, call `q = q.activate()` once before the first `for` loop (and never combine with the with-block, see the double-activate error).","Keep creation and consumption in the same scope so the with-block visibly encloses every iteration site."],"exampleFix":"// before\nq = DataQueue(producer)\nfor item in q:  # not activated -> ValueError\n    ...\n// after\nwith DataQueue(producer) as q:\n    for item in q:\n        ...","handlingStrategy":"validation","validationCode":"def iter_data_queue(q):\n    if not getattr(q, \"_activated\", False):\n        raise RuntimeError(\"activate the DataQueue (or use a with-block) before iterating\")\n    return iter(q)","typeGuard":"def queue_ready(q) -> bool:\n    return bool(getattr(q, \"_activated\", False))","tryCatchPattern":"try:\n    for item in q:\n        process(item)\nexcept ValueError as e:\n    if \"forgotten to use the DataQueue in a with block\" in str(e):\n        with q:\n            for item in q:\n                process(item)\n    else:\n        raise","preventionTips":["Always consume a DataQueue inside `with DataQueue(...) as q:`.","Pass the queue to helpers already inside the with-block, not before.","In notebooks, keep creation and iteration in the same cell or a single with-block."],"tags":["rl","data-queue","lifecycle","context-manager","usage-error"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}