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的更多相关文章

  1. Celery ---- 分布式队列神器 ---- 入门

    原文:http://python.jobbole.com/87238/ 参考:https://zhuanlan.zhihu.com/p/22304455 Celery 是什么? Celery 是一个由 ...

  2. Spring-data-redis: 分布式队列

    Redis中list数据结构,具有"双端队列"的特性,同时redis具有持久数据的能力,因此redis实现分布式队列是非常安全可靠的.它类似于JMS中的"Queue&qu ...

  3. 实现分布式队列ZooKeeper的实现

    一.背景 有一些时候,多个团队需要共同完成一个任务,比如,A团队将Hadoop集群计算的结果交给B团队继续计算,B完成了自己任务再交给C团队继续做.这就有点像业务系统的工作流一样,一环一环地传下去,直 ...

  4. 分布式队列神器 Celery

    Celery 是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任 ...

  5. 分布式队列ZooKeeper的实现

    一.背景 有一些时候,多个团队需要共同完成一个任务,比如,A团队将Hadoop集群计算的结果交给B团队继续计算,B完成了自己任务再交给C团队继续做.这就有点像业务系统的工作流一样,一环一环地传下 去, ...

  6. 借读:分布式锁和双写Redis

      本帖最后由 howtodown 于 2016-10-3 16:01 编辑问题导读1.为什么会产生分布式锁?2.使用分布式锁的方法有哪些?3.本文创造的分布式锁的双写Redis框架都包含哪些内容? ...

  7. 分布式队列Celery

    Celery是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任务 ...

  8. 消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用

    消息队列介绍.RabbitMQ&Redis的重点介绍与简单应用 消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下 ...

  9. 进击的Python【第十一章】:消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用

    消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下消息队列的基本思路. 还记得原来写过Queue的文章,不管是线程queu ...

随机推荐

  1. CUDA 显存操作:CUDA支持的C++11

    CUDA9的编译器和语言改进 使用CUDA 9,nvcc编译器增加了对C ++ 14的支持,其中包括新功能 通用的lambda表达式,其中使用auto关键字代替参数类型; auto lambda = ...

  2. 09C语言指针

    C语言指针 地址 地址就是数据元素在内存中的位置表示: &变量名 #include <stdio.h> int main(){ int aa; unsigned int bb = ...

  3. 学习SpringBoot中遇见的坑

    1. 在搭建SpringBoot HelloWorld 时项目结构应该这样: 而不能这样: 否则访问时出现错误页面: 原因:此时还不知道,先记录下来. --已解决2018/12/11,因为Spring ...

  4. post请求重定向到get请求问题

    springMVC默认重定向是get请求,我在方法注解中没有指定method是post还是get请求,这样就可以接收到post重定向来的请求,也可以接收到页面传来的get请求,如果要传参,可以使用mo ...

  5. 诊断:Goldengate OGG-01163 Bad column length

    故障现象: OGG- Bad column length () specified . 原因:源端修改了字段长度.虽然源端和目标端的长度已经通过DDL语句修改到一致,在extract进程未重启的情况下 ...

  6. C++ volatile关键字(转)

    文章来源: http://hedengcheng.com/?p=725 https://www.cnblogs.com/god-of-death/p/7852394.html 1.基本概念 volat ...

  7. js之获取html标签的值

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. session 与 cookie 区别-----https://segmentfault.com/a/1190000013447750?utm_source=tag-newest

    cookie数据存放在客户的浏览器上,session数据放在服务器上. cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗考虑到安全应当使用session. sessi ...

  9. Linux & Filesystem Hierarchy Standard

    Linux & Filesystem Hierarchy Standard The Filesystem Hierarchy Standard of Linux https://zhuanla ...

  10. [luoguP1156] 垃圾陷阱(DP)

    传送门 先按照时间排序 f[i][j] 表示 前i个物品高度为j时所剩余的最大能量 显然每个物品有堆和吃两种选择 状态转移看代码 代码 #include <cstdio> #include ...