具有多线程的Python文件扫描应用程序
这段代码中如何使用多线程并限制线程;示例 4 线程。
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, content_type="image/tiff", filename=fName)
scandirs('./')
提前致谢。
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, content_type="image/tiff", filename=fName)
scandirs('./')
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建大小为
n
的 Thread 对象池(列表),使用 is_alive 检查是否有任何完成,然后您可以在其中分配新的目录扫描。此外,您还应该子类化 Thread 类并定义一个 run 方法来适应您的问题,有关如何执行此操作的详细信息请参阅文档。
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 arun
method to adapt to your problem, details on how to do this are in the docs.首先 import Threading 然后 Threading.init() 你想把哪一部分放在线程名称中 run()(这是基类 Thread 的重写方法) 或 Thread(target="your threaded function" ,args=[]) 使用线程对象调用启动方法。
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.