Last active
November 22, 2018 15:52
-
-
Save SuoXC/db12971a6ef90c2b4f1c67ae4148778c to your computer and use it in GitHub Desktop.
gpu training util, to accelerate dataloader's speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from queue import Queue | |
from threading import Thread | |
class AsyncQueue: | |
def __init__(self, generator, cache_size=10): | |
self._q = Queue(maxsize=cache_size) | |
self._generator = generator | |
self._input_thread = Thread(target=self.append_to_queue) | |
self._input_thread.setDaemon(True) | |
self._input_thread.start() | |
def append_to_queue(self): | |
for item in self._generator: | |
self._q.put(item, block=True) | |
def __iter__(self): | |
return self | |
def __next__(self): | |
return self._q.get(block=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment