ldmpscript
2012-12-24 02:48 deepin
- ##########################################
- ## 线程扫描目录.
- ## scan_dir = ScanDir('/home')
- ## scan_dir.connect("scan-file-event",self.scan.. ..
- ## def scan_file_event(scan_dir, file_name):...
- class ScanDir(gobject.GObject):
- __gsignals__ = {
- "scan-file-event" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
- (gobject.TYPE_STRING,)),
- "scan-end-event" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
- ()),
- }
- def __init__(self, path):
- gobject.GObject.__init__(self)
- self.event = threading.Event()
- self.path = path
- self.__run()
- def pause(self): # 暂停线程
- self.event.clear()
- def start(self): # 开启线程
- self.event.set()
- def __wait(self):
- self.event.wait()
- def enter(self):
- gtk.gdk.threads_enter()
- def leave(self):
- gtk.gdk.threads_leave()
- def __scan(self, path):
- self.__wait()
- try:
- if os.path.isdir(path):
- for file_ in os.listdir(path):
- file_path = os.path.join(path, file_)
- for sub_file in self.__scan(file_path):
- yield sub_file
- else:
- yield path
- except:
- print "read file error!!"
- def __run(self):
- scan_th = threading.Thread(target=self.__run_func)
- scan_th.setDaemon(True)
- scan_th.start()
- def __run_func(self):
- for file_ in self.__scan(self.path):
- self.emit("scan-file-event", file_)
- self.emit("scan-end-event")
- if __name__ == "__main__":
- gtk.gdk.threads_init()
- def scan_file_event(scan_dir, file_name):
- gtk.gdk.threads_enter()
- label.set_label(file_name)
- gtk.gdk.threads_leave()
- def scan_end_event(scan_dir):
- gtk.gdk.threads_enter()
- label.set_label("%s扫描完毕"%(scan_dir.path))
- gtk.gdk.threads_leave()
- def start_btn_clicked(widget):
- scan_dir.start()
- def pause_btn_clicked(widget):
- scan_dir.pause()
- scan_dir = ScanDir("/")
- scan_dir.connect("scan-file-event", scan_file_event)
- scan_dir.connect("scan-end-event", scan_end_event)
- win = gtk.Window(gtk.WINDOW_TOPLEVEL)
- win.set_title("线程测试!!")
- vbox = gtk.VBox()
- hbox = gtk.HBox()
- start_btn=gtk.Button("开始")
- pause_btn=gtk.Button("暂停")
- hbox.pack_start(start_btn, False, False)
- hbox.pack_start(pause_btn, False, False)
- label = gtk.Label("...")
- vbox.pack_start(hbox, False, False)
- vbox.pack_start(label, False, False)
- win.add(vbox)
- win.connect("destroy", lambda w : gtk.main_quit())
- start_btn.connect("clicked", start_btn_clicked)
- pause_btn.connect("clicked", pause_btn_clicked)
- win.show_all()
- gtk.gdk.threads_enter()
- gtk.main()
- gtk.gdk.threads_leave()
Reply Like 0 View the author
因为本人要研究在线视频,要用到线程池等一些东西,所以就做点笔记什么的,大家不要以为是什么东西.
MFC界面线程详解: http://zhou24388.blog.163.com/bl ... 327201172992145405/
http://www.cnblogs.com/yinh/archive/2005/05/25/162150.aspx
线程中 队列 生产者 与 消费者队列的例子. 这个例子对于编写 线程池非常有帮助.
扫描目录的线程的暂停和继续.