from threading import Thread import time import random from queue import Queue from collections import deque #创建队列,设置队列最大数限制为3个 queue = Queue(3) #生产者线程 class Pro_Thread(Thread): def run(self): #原材料准备,等待被生产 tasks = deque([1, 2, 3, 4, 5, 6, 7, 8]) global queue while True: try: #从原材料左边开始生产 task = tasks.popleft() queue.put(task) print("生产", task, "现在队列数:", queue.qsize()) #休眠随机时间 time.sleep(random.random()) #如果原材料被生产完,生产线程跳出循环 except IndexError: print("原材料已被生产完毕") break #消费者线程 class Con_Thread(Thread): def run(self): global queue while True: if queue.not_empty: #通过get(),这里已经将队列减去了1 task = queue.get() time.sleep(2) #这里可能队列数已经空了,但是消费者手里还有正在消费的队列 #发出完成的信号,不发的话,join会永远阻塞,程序不会停止 queue.task_done() print("消费", task) else: break #r入口方法,主线程 def main(): Pro_1 = Pro_Thread() #把生产线程列为守护线程,否则主线程结束之后不会销毁该线程,程序不会停止,影响实验结果 Pro_1.setDaemon(True) #启动线程 Pro_1.start() for i in range(2): Con_i = Con_Thread() # 把两个消费者线程列为守护线程,否则主线程结束之后不会销毁该线程,程序不会停止,影响实验结果 Con_i.setDaemon(True) #启动线程 Con_i.start() global queue #这里休眠一秒钟,等到队列有值,否则队列创建时是空的,主线程直接就结束了,实验失败,造成误导 time.sleep(1) #接收信号,主线程在这里等待队列被处理完毕后再做下一步 queue.join() #给个标示,表示主线程已经结束 print("主线程结束") if __name__ == '__main__': main()
wrong:
#coding:utf8
#因为注释中含有中文,所以在代码文件中首先要指明编码方式为UTF-8
#多线程处理Ping
import db1
import sys
reload(sys)
sys.setdefaultencoding('utf8')#指定代码编码格式
sys_encoding = sys.getfilesystemencoding()
from threading import Thread
import subprocess
from Queue import Queue
import re
import time
def files_ip(ips):
f = '';
try :
f = open('C:\\Users\\cctv1\\Desktop\\PING_IP_LIST.txt','rb')
for line in f:
str = line.strip('\n\r')
ips.append(str)
finally:
if f:
f.close()
def pingme(i,queue,ipdatavalue):
while not queue.empty():
ip=queue.get()
print 'Thread %s pinging %s' %(i,ip)
cmd = 'ping -n %s %s' % (1, ip)
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
result = p.stdout.read()
regex = re.findall('100%', result)
ip_state = '1'
ipmsg=''
avg_time = 9999
if len(regex) == 0:
ipmsg = "\033[31m%s UP\033[0m\r" % (ip)
reg_receive = '平均\s=\s\d'
out = str(result.decode('gbk'))
match_receive = re.search(reg_receive,out)
print('^^^')
print(match_receive)
if match_receive:
match_receivegrp = str(match_receive.group()).decode('utf-8')
avg_time = int(match_receivegrp[5:])
else:
avg_time = 9999
else:
ipmsg = "\033[32m%s DOWN\033[0m\r" % (ip)
ip_state='0'
avg_time = 9999
ipdatavalue.append((ip,ip_state,ipmsg,avg_time))
queue.task_done()
def start_run_ip(ips,batch_no):
ipdatavalue = []
num_threads=30
q=Queue()
for ip in ips:
q.put(ip)
#start num_threads threads
for i in range(num_threads):
t=Thread(target=pingme,args=(i,q,ipdatavalue))
#print i
t.setDaemon(True)
t.start()
q.join();
insertsql = "INSERT INTO monitor_ip_log (ip_address,ip_state,batch_no,date_time,state_remark,avg_time) VALUES (%s,%s,"+batch_no+",NOW(),%s,%s)"
db1.insert_data_batch(insertsql,ipdatavalue)
def t_main():
time_start = time.time()
#ip列表
ips = []
files_ip(ips)
#批次号
batch_no = db1.get_date_time()
start_run_ip(ips,batch_no)
print 'Done'
time_end = time.time()
time_cost = time_end - time_start
print("%.2f s" % (time_cost))