python多线程队列使用
2014-11-03 16:09
495 查看
#线程1 -> 队列 -> 线程2 -> url_web
#
#
#
本文出自 “小盒” 博客,请务必保留此出处http://zhangxiaohe.blog.51cto.com/7821029/1571305
#
#
#
#!/usr/bin/env python #! coding=utf-8 #!by=xiaohe import Queue,threading,time,random from moniItems import mon import urllib2 from flask import Flask,request,json queueLock = threading.Lock() class t1(threading.Thread): def __init__(self,t_name,queue): threading.Thread.__init__(self,name=t_name) self.data = queue def run(self): #for i in range(5): while True: queueLock.acquire() if self.data.empty(): print time.ctime(),self.getName() self.data.put(mon().runAllGet()) time.sleep(1) queueLock.release() else: queueLock.release() #print time.ctime(),self.getName(),"end" class t2(threading.Thread): def __init__(self,t_name,queue): threading.Thread.__init__(self,name=t_name) self.data=queue def run(self): #for i in range(5): while True: queueLock.acquire() if not self.data.empty(): vai = self.data.get() #print self.getName() , vai a=urllib2.Request("http://reboot:8088", json.dumps(vai), {'Content-Type': 'application/json'}) urllib2.urlopen(a) queueLock.release() else: queueLock.release() def mai(): queue = Queue.Queue(5) tt1 = t1('shou',queue) tt2 = t2('fa',queue) tt1.start() tt2.start() tt1.join() tt2.join() if __name__=='__main__': mai()
本文出自 “小盒” 博客,请务必保留此出处http://zhangxiaohe.blog.51cto.com/7821029/1571305
相关文章推荐
- python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)
- python模块之threading: 在python中使用多线程
- Python多线程使用2009-12-30
- [Python]多线程入门 --thread使用
- python 多线程 +队列下载资源
- Linux多线程系列-2-条件变量的使用(线程安全队列的实现)
- python模块之threading: 在python中使用多线程
- Python:数组、队列及堆栈的使用(list用法)--转
- 使用NSOperationQueue简化多线程开发和队列的优先级
- Python:使用threading模块实现多线程编程一[综述]
- [ios2]使用NSOperationQueue简化多线程开发和队列的优先级 【转】
- python下多线程的思考和Queue的使用
- python使用多线程
- python 多线程使用
- python 进行多线程编程,使用Semaphore控制线程数
- 使用Python多线程抓取并验证代理
- 使用NSOperationQueue简化多线程开发 (二) 队列的优先级
- python多线程的基本使用
- Python代理抓取并验证使用多线程实现
- python:threading多线程模块-使用Queue模块保持线程同步