Python file scan application with multithreading
How to use multithreading in this code and limit the thread; example 4 thread.
import os
import glob
from pymongo import Connection
from gridfs import GridFS
db = Connection().microfilm
fs = GridFS(db)
def scandirs(path):
for currentFile in glob.glob( os.path.join(path, '*') ):
if os.path.isdir(currentFile):
print 'Entering directory: ' + currentFile
scandirs(currentFile)
base, ext = os.path.splitext(currentFile)
if ( ext == '.tif'):
print "Processing file: " + currentFile
fName = os.path.basename(currentFile)
with open(currentFile) as gfsFile:
oid = fs.put(gfsFile, conten开发者_StackOverflow社区t_type="image/tiff", filename=fName)
scandirs('./')
Thanks in advance.
Create a pool(list) of Thread objects of size n
, use is_alive to check if any is finished, so then you can assign a new directory scan in it.
Also you should subclass the Thread
class and define a run
method to adapt to your problem, details on how to do this are in the docs.
first import Threading then Threading.init() which part u want to put in thread name it run()(This is the overridden method of base class Thread) or Thread(target="your threaded function",args=[]) call start method with thread object.
精彩评论