Skip to content

Instantly share code, notes, and snippets.

@SuoXC
Last active November 22, 2018 15:52
Show Gist options
  • Save SuoXC/db12971a6ef90c2b4f1c67ae4148778c to your computer and use it in GitHub Desktop.
Save SuoXC/db12971a6ef90c2b4f1c67ae4148778c to your computer and use it in GitHub Desktop.
gpu training util, to accelerate dataloader's speed
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