将分布式-队列的实现交给redis
import requests
import re
import time
from redis import Redis
import threading REDIS_HOST, REDIS_PORT, PASSWORD = '192.168.2.51', '6379', 'mypwd'
rds = Redis(host=REDIS_HOST, port=REDIS_PORT, password=PASSWORD) f, url_l, filter_replace_l = 'kwaddress_address_20180227.json', [], ['\n', '\t', ' ']
with open(f,'r',encoding='utf-8') as fr:
for i in fr:
try:
ii =i.split('address":"')[-1].split('"}')[0]
#url_l.append(ii)
rds.sadd('chk_url_all', ii)
except Exception as e:
print(e) 去内存,持久化
import pymysql
from redis import Redis
import time h, pt, u, p, db = '192.168.2.210', 3306, 'root', 'nfwt&2016', 'xl_product_DONOT_REMOVE' def mysql_fetch(sql, res_type='tuple'):
global h, pt, u, p, db
try:
conn = pymysql.connect(host=h, port=pt, user=u, passwd=p, db=db, charset='utf8')
except Exception as e:
print(e)
return ()
if res_type == 'dic':
cursor = conn.cursor(pymysql.cursors.DictCursor)
else: cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
return cursor.fetchall() def mysql_write(sql):
global h, pt, u, p, db
try:
conn = pymysql.connect(host=h, port=pt, user=u, passwd=p, db=db, charset='utf8')
except Exception as e:
print(e)
return 1
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
return 0 REDIS_HOST, REDIS_PORT, PASSWORD = '192.168.2.51', '6379', 'mypwd'
rds = Redis(host=REDIS_HOST, port=REDIS_PORT, password=PASSWORD) while True:
s_404 = [i.decode('utf-8') for i in rds.smembers('chk_url_404')]
for i in s_404:
sql = 'SELECT id FROM chk_url_404 WHERE url="{}" '.format(i)
try:
r = mysql_fetch(sql)
print(sql)
except Exception as e:
print(e)
continue
if len(r) == 0:
sql = 'INSERT INTO chk_url_404 (url,indb_time) VALUES ("{}","{}")'.format(i, int(time.time()))
try:
mysql_write(sql)
print(sql)
except Exception as e:
print(e)
continue
time.sleep(60 * 15) 多线程
import requests
import re
import time
from redis import Redis
import threading REDIS_HOST, REDIS_PORT, PASSWORD = '192.168.2.51', '6379', 'mypwd'
rds = Redis(host=REDIS_HOST, port=REDIS_PORT, password=PASSWORD) # f, url_l, filter_replace_l = 'DISTINCT_url.txt', [], ['\n', '\t', ' ']
# with open(f, 'r', encoding='utf-8') as fr:
# for i in fr:
# try:
# for ii in filter_replace_l:
# i = i.replace(ii, '')
# rds.sadd('chk_url_all', i)
# except Exception as e:
# print(e) def tf():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
while True:
try:
url_ori = rds.spop('chk_url_all').decode('utf-8')
s = 'http://'
if s not in url_ori:
url = '{}{}'.format(s, url_ori)
print(url)
r = requests.get(url, headers=headers, timeout=50)
print(r.status_code)
sc = r.status_code
if sc == 404:
print(sc)
rds.sadd('chk_url_404', url_ori)
except Exception as e:
print(e) class MyThread(threading.Thread):
def __init__(self, func, args=None, name=None):
threading.Thread.__init__(self)
self.func, self.args, self.name = func, args, name def run(self):
if self.args==None:
self.func()
else:
self.func(self.args) tl = []
for i in range(10):
thread_instance = MyThread(tf)
tl.append(thread_instance) for t in tl:
t.setDaemon = False
t.start()
for t in tl:
t.join() 发布代码至多台机器
redis 自典型 支持更新
rds = return_redis(redis_key)
rds.hset(my_dict_name, uid, uid_info)
将分布式-队列的实现交给redis的更多相关文章
- Celery ---- 分布式队列神器  ----  入门
		
原文:http://python.jobbole.com/87238/ 参考:https://zhuanlan.zhihu.com/p/22304455 Celery 是什么? Celery 是一个由 ...
 - Spring-data-redis: 分布式队列
		
Redis中list数据结构,具有"双端队列"的特性,同时redis具有持久数据的能力,因此redis实现分布式队列是非常安全可靠的.它类似于JMS中的"Queue&qu ...
 - 实现分布式队列ZooKeeper的实现
		
一.背景 有一些时候,多个团队需要共同完成一个任务,比如,A团队将Hadoop集群计算的结果交给B团队继续计算,B完成了自己任务再交给C团队继续做.这就有点像业务系统的工作流一样,一环一环地传下去,直 ...
 - 分布式队列神器 Celery
		
Celery 是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任 ...
 - 分布式队列ZooKeeper的实现
		
一.背景 有一些时候,多个团队需要共同完成一个任务,比如,A团队将Hadoop集群计算的结果交给B团队继续计算,B完成了自己任务再交给C团队继续做.这就有点像业务系统的工作流一样,一环一环地传下 去, ...
 - 借读:分布式锁和双写Redis
		
本帖最后由 howtodown 于 2016-10-3 16:01 编辑问题导读1.为什么会产生分布式锁?2.使用分布式锁的方法有哪些?3.本文创造的分布式锁的双写Redis框架都包含哪些内容? ...
 - 分布式队列Celery
		
Celery是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任务 ...
 - 消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用
		
消息队列介绍.RabbitMQ&Redis的重点介绍与简单应用 消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下 ...
 - 进击的Python【第十一章】:消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用
		
消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下消息队列的基本思路. 还记得原来写过Queue的文章,不管是线程queu ...
 
随机推荐
- cstring to utf8
			
char* UnicodeToUtf8(CString unicode) { int len; len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)unico ...
 - CPU 的寻址方式
			
来源https://baike.baidu.com/item/%E5%AF%BB%E5%9D%80%E6%96%B9%E5%BC%8F/3210621?fr=aladdin 寻址方式就是处理器根据指令 ...
 - 【原】CentOS release 6.2 安装mysql
			
1. yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (C ...
 - 洛谷——P2236 [HNOI2002]彩票
			
P2236 [HNOI2002]彩票 给你$m$个数,从中挑$n$个数,使得这$n$个数的倒数之和恰好等于$\frac{x}{y}$ 常见的剪纸思路: 如果当前的倒数和加上最小可能的倒数和$>$ ...
 - static private 与 final 的用法总结
			
1.static表示静态.他是属于类的.可以在本身类里直接调用,或在其它类里用类名.方法名调用.不加static表示是实例的方法,必须用实例来调用.在本类里也一样,必须用实例调用 2.private是 ...
 - ubuntu环境搭建DNS服务器
			
1 安装bind9 apt install bind9 2 修改 named.conf.local,添加要解析的域名及对应的域名配置文件 zone "test.cn"{ type ...
 - STM32 内存管理实验
			
参考原文<STM32F1开发指南> 内存管理简介 内存管理,是指软件运行时对计算机内存资源的分配和使用的技术.最主要的目的是如何高效.快速的分配,并且在适当的时候释放和回收内存资源.内存管 ...
 - AD7606
			
在只给芯片的RANGE和PAR_SER引脚上电(不给芯片加电)的时候,芯片严重发热. 改回给芯片加电,发热消失,芯片正常工作,芯片没有损坏. 版权声明:本文为博主原创文章,未经博主允许不得转载.
 - jmeter 性能测试
			
1. Ramp-up Period(in seconds)代表多长时间内启动所有线程 2. Aggregate Report Samples:总共发给服务器的请求数量 Average:单个请求的平均响 ...
 - RequestMapping_请求方式
			
[映射请求参数.请求方法或请求头] 1.@RequestMapping 除了可以使用请求URL 映射请求外,还可以使用请求方法.请求参数及请求头映射请求. 2.@RequestMapping 的val ...