python多线程队列使用

#线程1 -> 队列 -> 线程2 -> url_web

#

#

#

#!/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()


你可能感兴趣的:(多线程,python)