Python中队列和线程的使用
2013-02-05 18:56
555 查看
Python中对队列和线程的操作,需要使用模块:Queue 和 threading。其中,Queue模块中提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。可以使用队列来实现线程间的同步。
简单的研究了一下使用方法,写了一个小例子,代码如下:
功能:从文件中读取每一行,放入队列中,用线程输出
需要注意的几点:
1、"parser.daemon = True" 必须在线程Start之前使用,否则可能出现RuntimeError异常。它的初始值是从主线程继承的,主线程一般不是守护线程,所以它的默认值为False
2、每次从Queue中取出一个元素,处理完成之后,最好调用一下"Queue.task_done()"
参考资料:
1、http://docs.python.org/2/library/queue.html
2、http://docs.python.org/2/library/threading.html
3、http://www.ibm.com/developerworks/cn/aix/library/au-threadingpython/
4、http://www.2cto.com/kf/201109/104515.html
5、http://www.cnblogs.com/holbrook/archive/2012/03/15/2398060.html
6、http://developer.51cto.com/art/201003/186160.htm
简单的研究了一下使用方法,写了一个小例子,代码如下:
#!/bin/env python #coding=utf-8 import os import sys import threading import time import Queue global g_thread_num global g_pages_queue class Parser(threading.Thread): def __init__(self, threadname, pagequeue): threading.Thread.__init__(self, name = threadname) self.queue = pagequeue def run(self): print("Parser::thread running...") while True: if self.queue.empty(): break else: time.sleep(0.05) page_file = self.queue.get() #do some thing here print self.name, self.ident, page_file self.queue.task_done() time.sleep(0.05) return def GetPagesFromFile(file_name): if len(file_name) == 0 or file_name == None: print("GetPagesFromFile::input file is null") sys.exit(-1) fp = open(file_name) for url in fp.readlines(): g_pages_queue.put(url.strip('\n')) fp.close() return g_pages_queue.qsize() def RunFileInterface(file_name): if len(file_name) == 0: return page_nb = GetPagesFromFile(file_name) print page_nb for i in range(g_thread_num): parser = Parser("parser " + str(i), g_pages_queue) parser.daemon = True parser.start() #parser.join() g_pages_queue.join() print("RunFileInterface::Done......") return if __name__ == '__main__': g_thread_num = 2 g_pages_queue = Queue.Queue() if len(sys.argv) == 2 and sys.argv[1] == '-db': print("Main::Run DB Interface!") #RunDBInterface() elif len(sys.argv) >= 2 and sys.argv[1] == '-f': page_files = "" if len(sys.argv) == 3: page_files = str(sys.argv[2]) if os.path.exists(page_files) and os.path.isfile(page_files): print("Main::Run File Interface!") RunFileInterface(page_files) else: print("Main::File Not Exists!") sys.exit(0)
功能:从文件中读取每一行,放入队列中,用线程输出
需要注意的几点:
1、"parser.daemon = True" 必须在线程Start之前使用,否则可能出现RuntimeError异常。它的初始值是从主线程继承的,主线程一般不是守护线程,所以它的默认值为False
2、每次从Queue中取出一个元素,处理完成之后,最好调用一下"Queue.task_done()"
参考资料:
1、http://docs.python.org/2/library/queue.html
2、http://docs.python.org/2/library/threading.html
3、http://www.ibm.com/developerworks/cn/aix/library/au-threadingpython/
4、http://www.2cto.com/kf/201109/104515.html
5、http://www.cnblogs.com/holbrook/archive/2012/03/15/2398060.html
6、http://developer.51cto.com/art/201003/186160.htm
相关文章推荐
- Python入门:队列在线程中的使用
- 使用队列和线程对现有asp.net动态页处理进行资源控制
- Python线程下使用锁的技巧分享
- [C++11 并发编程] 12 使用条件变量创建线程间安全的队列
- python线程的使用模式
- python多线程编程(3): 使用互斥锁同步线程
- python 进程,线程的使用方法
- iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组
- python使用队列实现生产者消费者
- python在子线程中使用WMI报错-2147221020-moniker,i,bindCTX=pythoncom.MKParseDisplayName(Pathname)
- 重新学习python线程应用使用thread类,改写程序
- python 栈和队列(使用list实现)
- 使用python测测你的系统最多能创建多少个线程
- python线程优先级队列( Queue)
- python数据结构与算法——小猫钓鱼(使用队列)
- 系统进程与线程关系以及使用Python获取的方法
- python使用装饰器和线程限制函数执行时间的方法
- python 线程锁的使用
- java 使用线程模拟 队列实现
- Python3-线程应用队列(生产者与消费者模式--多生产者VS多消费者)