# SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: LicenseRef-NvidiaProprietary # # NVIDIA CORPORATION, its affiliates and licensors retain all intellectual # property and proprietary rights in and to this material, related # documentation and any modifications thereto. Any use, reproduction, # disclosure or distribution of this material and related documentation # without an express license agreement from NVIDIA CORPORATION or # its affiliates is strictly prohibited. """This package is responsible for defining data thread and work queue. Data thread is a daemon thread that plays the role of a consumer. It will consume the works (provided by the data center) in the work queue. """ from queue import Queue from threading import Event, Thread class WorkQueue(Queue): def __init__(self): super().__init__(maxsize=0) self.consumable = Event() def add_work(self, work): self.put(work) self.consumable.set() class DataThread(Thread): def __init__(self, work_queue): Thread.__init__(self) self.work_queue = work_queue def run(self): while True: self.work_queue.consumable.wait() if not self.work_queue.empty(): work = self.work_queue.get() work() # start working self.work_queue.task_done() if self.work_queue.empty(): self.work_queue.consumable.clear() # Instantiate singletons work_queue = WorkQueue() data_thread = DataThread(work_queue) data_thread.daemon = True data_thread.start()