Skip to content
Browse files

First attempt at processing input files on background thread. Status …

…call does not work because thread is not shared between requests. Needs to be made a global singleton.
  • Loading branch information...
1 parent 21bb7bb commit 083e026150f421e91864e1f29f23712b7a6b0f85 @MusikPolice committed Sep 4, 2012
Showing with 45 additions and 3 deletions.
  1. +45 −3 musik.py
View
48 musik.py
@@ -1,12 +1,15 @@
+from multiprocessing import Process
import os, os.path
import re
+import time
+from threading import Lock
import cherrypy
from cherrypy.process import wspbus, plugins
from mako.template import Template
from mako.lookup import TemplateLookup
-
+
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
@@ -91,15 +94,54 @@ def commit_transaction(self):
# provides functionality for adding new media to the database
+# TODO: There's a scoping issue somewhere in here. processimports() seems to run and update totalItems, but status() can't see the updates
class Import:
+ # a worker thread responsible for importing media into the application
+ importmedia_process = None
+
+ totalItems = 0
+ currentItem = 0
+ itemLock = Lock()
+
@cherrypy.expose
def directory(self, path):
+
+ #TODO: enumerate the directory at path into a list of files
+ #TODO: for each file - if file mime type is valid, add it to database import table
+ #TODO: ensure that importmedia_process thread is started - it will process the import table
+
+ if self.importmedia_process is None:
+ self.importmedia_process = Process(target=self.processimports)
+ self.importmedia_process.start()
+
return "Importing the directory %s" % path
@cherrypy.expose
- def file(self, path):
- return "Importing the file %s" % path
+ def status(self):
+ #TODO: this method should return the status of the importer, including file currently being processed, and completion % of import job
+ if self.importmedia_process is None or self.importmedia_process.is_alive()==False:
+ return "Nothing is being imported at this time."
+ else:
+ with self.itemLock:
+ cherrypy.log(str(self.currentItem))
+ return "Currently processing item %s of %s" % (self.currentItem, self.totalItems)
+
+ def processimports(self):
+ #TODO: pull all files to be processed from the database import table
+ #TODO: foreach file - actually add it to the database correctly and delete it from the import table
+ with self.itemLock:
+ self.totalItems = 100
+ self.currentItem = 0
+
+ for x in range(0, 100):
+ with self.itemLock:
+ self.currentItem += 1
+ cherrypy.log("Current Item is %s" % str(self.currentItem))
+ time.sleep(1)
+
+ self.importmedia_process = None
+ return None
# defines an api with a dynamic url scheme composed of /<tag>/<value>/ pairs

0 comments on commit 083e026

Please sign in to comment.
Something went wrong with that request. Please try again.