python 多进程实例 进程间的通信
来源:互联网 发布:芙丽芳丝洗面奶知乎 编辑:程序博客网 时间:2024/05/21 14:44
import httplib2from multiprocessing import Lock, Process, Queue, current_processdef worker(work_queue, done_queue): try: for url in iter(work_queue.get, 'STOP'): status_code = print_site_status(url) done_queue.put("%s - %s got %s." % (current_process().name, url, status_code)) except Exception, e: done_queue.put("%s failed on %s with: %s" % (current_process().name, url, e)) return Truedef print_site_status(url): http = httplib2.Http(timeout=10) headers, content = http.request(url) return headers.get('status', 'no response')def main(): sites = ( 'http://penny-arcade.com/', 'http://reallifecomics.com/', 'http://sinfest.net/', 'http://userfriendly.org/', 'http://savagechickens.com/', 'http://xkcd.com/', 'http://duelinganalogs.com/', 'http://cad-comic.com/', 'http://samandfuzzy.com/', ) workers = 4 work_queue = Queue() done_queue = Queue() processes = [] for url in sites: work_queue.put(url) for w in xrange(workers): p = Process(target=worker, args=(work_queue, done_queue)) p.start() processes.append(p) work_queue.put('STOP') for p in processes: p.join() done_queue.put('STOP') for status in iter(done_queue.get, 'STOP'): print statusif __name__ == '__main__': main()
阅读全文
0 0
- python 多进程实例 进程间的通信
- python 多进程通信模块实例 代码
- python 进程间通信
- python : 进程间通信
- python 进程间通信
- python 进程间通信
- python 进程间通信
- python进程间通信
- 【Python】进程间通信
- 基于Message的进程间通信实例
- 进程间通信---AIDL的使用实例
- 进程间通信---AIDL的使用实例
- Python 多进程实例
- python 多进程实例
- python 多进程实例
- Python下的进程间通信-管道
- Python多进程通信Queue、Pipe、Value、Array实例
- 进程间通信(IPC) 实例
- android 使用opengl开发VR的使用
- 面试题65:滑动窗口的最大值
- 指定maven的编译版本和编码
- DUTOJ 1110 比赛成绩 DFS 并查集
- POJ 2796:Feel Good (单调栈)
- python 多进程实例 进程间的通信
- VS2015动态库编程之MFC扩展动态库(下)
- python18运维应用
- C#中Equal与==的使用原则
- HttpClient爬取网站及图片
- Python语言学习之一:环境搭建,执行方式及主模块和非主模块
- Tensorflow学习——ReLu
- Linux下线程
- Codeforces 670D2 Magic Powder-2