I don't know if this helps any (or if even applicable to what you
need), but this work for me s it serves a multitude of purposes,
though mainly my task request queuing system. I trimmed it down to the
bare bone so it would make more sense.
* in a nutshel, it polls an arbitrary directory structure
(recursively) and simply keeps tracks of files and directories, coming
in and going out. It uses modified time stamp and compares that to
others. So, it has built-in prioritization (in a first-in, first-out
system). That's it.
* but with that, we can add tones of stuff to make it work for us.
Here are a few examples:
* it already takes care of keeping track of in-coming and out-
going traffic. So those files can contain stuff (like data, data-
types, pickled objects, script, string that can be executed (one of
the reasons we like python), it could also be...? can you guess? it
can be a bunch of CSV files that some web2py instance (or script using
DAL) dumped there after exporting its DB to CVS... (here, because
maybe the root folder is actually under /applications/appName/static/
Q) we can do something like have a controller check for that. This
controller could potentially say "hey, there's a bunch of CSV files, I
will have myApp import them!".
Because, we poll recursively, we could take advantage of that and give
the directories under the /Q some purpose, like the directory path
could represent something like a network path, or a family tree, or an
organization's "in-house mail system" (well, it could be ;) ).
Actually, a little while ago, I wanted to use this to organize my
build-time generated DB & tables. Would have been dandy, but seems not
possible :(. But another interesting idea with queuing could be this:
the folder structure could represent the decision flow of some sort of
workflow.... something like this:
a new file is dropped in root.submit
then processed by WF. it could now be expected to be dropped at some
point in the "processing" folder where "processor.py polls for files
and evaluates when found (or could go to the rejected folder @
anytime). so now the file is moving down the directory structure:
root.submit.processing
maybe now processor.py determines that it is a valid request, so it
droppes the file in a "pending" folder. so the structure has a new
layer:
root.submit.processing.pending_human_approval
here the Q notices the new file in a folder called
"pending_human_approval", reads it and loads instructions & sees who
the user's human manager is and notifies the manager that he needs to
approve or reject a workflow item.
the manager receives the notice and then logs in to the web2py app,
sees the 'pending_human_approval" message. He opens and clicks on
"approve" which will have the affect of moving the file to the
"approved folder. So now this flow's structure now looks like this:
root.submit.processing.pending_human_approval.approved
so now, our initial submitter is happy because he received a
"decision_taken" email form the automated workflow. So,he now logs in,
goes to his "work_items" and clicks on the
"submit_time_sheet_to_payrol" button because his manager has approved
this employee's request to take this year's Christmas day as
unpaid_time_off! :)
This is a queuing system because maybe there are another 200 employees
who want the christmas day off.
So, the recursive queuing system, that keeps track of files and folder
(& time stamps everything) can be used to handle a workflow's queuing
system (in fact be at the heart of it)...
anyways, just having fun thinking about ;) the script is below, and
does queuing :)
hope it helps, or at least provided minimal amusement ;)
Mart
from __future__ import nested_scopes
import os, time
import threading
def Qstructure (rPath,funcCheckQ):
_all = {}
def checkQ(unused, dirName, fileset):
for fName in fileset:
path = os.path.join(dirName, fName)
try:
t = os.stat(path)
except os.error as oErr:
continue
mtime = newOrModified.get(path)
if mtime is not None:
del newOrModified[path]
if t.st_mtime > mtime:
modifiedFileList.append(path)
else:
modifiedFileList.append(path)
_all[path] = t.st_mtime
patrolStructure = False
while True:
modifiedFileList = []
newOrModified = _all.copy()
_all = {}
for path in rPath:
os.path.walk(path, checkQ, None)
deletedFileList = newOrModified.keys()
if patrolStructure:
patrolStructure = False
elif modifiedFileList or deletedFileList:
patrolStructure = funcCheckQ(\
modifiedFileList\
,deletedFileList)
time.sleep(1.0)
def startCheckQ():
def checkQ(incoming,outgoing):
'''---
display the ins and outs
---'''
for item in incoming:print('in {0}'.format(item))
print('out {0}'.format(outgoing))
'''---
set the root ---'''
wDir = os.chdir('/Users/mart/dev_mart/')
Qstructure('.',checkQ,1.0)
if __name__ == '__main__':
startCheckQ()