summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichaël Ball <michael.ball@gmail.com>2017-08-19 11:18:28 +0100
committerMichaël Ball <michael.ball@gmail.com>2017-08-19 11:18:28 +0100
commit3d0bf539ca2909ebf1719d3e35afc778d69358dc (patch)
tree264cbb33a642681c54c5ec8cc52b85f77dd5eda1
parentd371717e04e116ed140698195c2d1f089c09b65f (diff)
Manage greenlets in a group
-rwxr-xr-xlibrary.py19
1 files changed, 13 insertions, 6 deletions
diff --git a/library.py b/library.py
index 272e9e5..c03c858 100755
--- a/library.py
+++ b/library.py
@@ -3,10 +3,11 @@
import logging
import os
-from gevent import joinall, monkey, queue, sleep, spawn
+from gevent import monkey, queue
+from gevent.pool import Group
import mutagen
import six
-from six.moves import configparser
+from six.moves import configparser, range
from db.db_manager import DbManager
from models.track import Track
@@ -44,12 +45,12 @@ class MediaLibrary(object):
file_queue (Queue[str]): A queue containing file paths.
"""
- while not file_queue.empty():
+ try:
path = file_queue.get()
metadata = mutagen.File(path, easy=True)
Track.store(_u(path), metadata, self.__database)
-
- sleep(0)
+ except queue.Empty:
+ pass
def run(self, path=None):
"""Store all tracks located in the supplied path.
@@ -102,7 +103,13 @@ class MediaLibrary(object):
file_queue.put(file_path)
_LOGGER.info("Storing tracks")
- joinall([spawn(self.store_track_task, file_queue)] * 18)
+
+ tasks = Group()
+ for i in range(file_queue.qsize()):
+ tasks.spawn(self.store_track_task, file_queue)
+
+ tasks.join()
+
_LOGGER.info("Done")
def delete_file(self, path):