def __init__(self, host): self.client = Redis(host=host)
import datetime from flask import Blueprint, request, jsonify from redis import Redis from db.query_db import query_db_outside from .sql import query from SqlTask.SqlTask import run import time import uuid import __main__ task = Blueprint('task', __name__) redis = Redis() @task.route('/add/', methods=['GET', 'POST']) def add_task(): taskid = str(uuid.uuid1()) #request.form.get('taskid') category = request.form.get('category') owner = request.form.get('owner') email = request.form.get('email') description = request.form.get( 'description') # The description of the task tag = request.form.get('tag') # The tag of the task enabled = eval(request.form.get('enabled')) # Whether the available freqency = request.form.get('freqency') task_type = request.form.get('task_type') threshold = int( request.form.get('threshold') ) # The threshold of result for sending the notice to owner
from frame import SpiderFrame from requests import exceptions from json import loads as json_lds from time import sleep from redis import Redis import config logger = SpiderFrame.logger html_downloader = SpiderFrame.HtmlDownloader() url_manager = SpiderFrame.UrlManager(use_redis=config.USE_REDIS, db_set_name=config.COMMENT_SET) data_saver = SpiderFrame.DataSaver(db_name=config.DB_NAME, set_name=config.COMMENT_SET) redis = Redis(host=config.REDIS_HOST, port=config.REDIS_PORT, password=config.REDIS_PASSWORD) def spider(answer_id: str) -> None: # 增量爬取评论 offset = config.MONGO_DOC_LIMIT logger.info("Get comments for answer id: {0}".format(answer_id)) url = "https://www.zhihu.com/api/v4/answers/{}/root_comments?limit=10&offset=0&order=normal&status=open" \ .format(answer_id) res = html_downloader.download(url) res = json_lds(res) if not redis.keys(answer_id): redis.set(answer_id, url)
from redis import Redis redis_connection = Redis(decode_responses=True, db=0) # Przekazywanie w komendzie eval ciało skryptu napisanego w lua. Redis odbiera kod, # wykonuje i zwraca rezultat. Wyświetlenie słowa test. Drugi argument eval to 0, # określa ilość argumentów które można przekazać do skrypti/ Lua ideksuje od 1, # wszystko przekazane jest do tabeli argv. script =""" return "testuje" """ print(redis_connection.eval(script,0))
def get_redis(): if not hasattr(g, 'redis'): g.redis = Redis(host="redis", db=0, socket_timeout=5) return g.redis
from flask import Flask, request, jsonify from redis import Redis from services.product_event_handler import emit_product_order app = Flask(__name__) redis = Redis(host='redis', port=6379) @app.route('/create', methods=['POST']) def create(): name = request.form['name'] price = request.form['price'] redis.mset({name: price}) return jsonify({'name':name, 'price':price}), 201 @app.route('/buy', methods = ['POST']) def buy(): name = request.form['name'] price = redis.get(name) emit_product_order(name) return jsonify({'receipt': 'Thanks for ordering! Your total comes to: ' + str(price)}), 200 if __name__ == '__main__': app.run(host='0.0.0.0', debug=True, port=3002)
from redis import Redis redis_connection = Redis(decode_responses=True) redis_connection.set("key", "value") # Set the value at key name to value redis_connection_1 = Redis( decode_responses=True, db=1) # not available on db1, because default is db0 # and on db0 exists an value of 'key' print(redis_connection_1.get("key")) print(redis_connection.get("key"))
import time from redis import Redis from threadpool_executor_shrink_able import ThreadPoolExecutorShrinkAble from threadpool_executor_shrink_able.sharp_threadpoolexecutor import set_thread_pool_executor_shrinkable,show_current_threads_num """ 引入redis在不同时候慢慢放入任务 和隔很久加入孺人,这样才方便测试 线程池自动扩张和减小。 """ r = Redis(decode_responses=True) pool = ThreadPoolExecutorShrinkAble(100) set_thread_pool_executor_shrinkable(2, 10) print(pool.MIN_WORKERS, pool.KEEP_ALIVE_TIME) show_current_threads_num(1) def f(x): time.sleep(0.2) # y = ['我' * 1000 * 1000 *100] y = '我' * 1000 * 1000 * 100 print(x) while 1: xx = r.blpop('test_adjust_task') pool.submit(f, xx[1])
import sys import sqlite3 import string import random import urllib.request from flask import Flask, g, request, redirect, render_template, session, abort from flask_session import Session from contextlib import closing from redis import Redis app = Flask(__name__) app.config['SESSION_TYPE'] = 'redis' app.config['SESSION_REDIS'] = Redis(host='redis', port=6379) Session(app) DATABASE = './data/database.db' USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36' def connect_db(): return sqlite3.connect(DATABASE) def id_generator(size=7, chars=string.ascii_letters + string.digits): return ''.join(random.choice(chars) for _ in range(size)) def get_title(url): req = urllib.request.Request(url, data=None, headers={'User-Agent': USER_AGENT})
from django.db import models from django.conf import settings from redis import Redis redis = Redis(settings.REDIS_HOST, port=int(settings.REDIS_PORT)) class EurovisionCelebrities(models.Model): COUNTRIES = [ ('AF', 'Afghanistan'), ('AL', 'Albania'), ('DZ', 'Algeria'), ('AX', 'Aland Islands'), ('AS', 'American Samoa'), ('AI', 'Anguilla'), ('AD', 'Andorra'), ('AO', 'Angola'), ('AN', 'Antilles - Netherlands'), ('AG', 'Antigua and Barbuda'), ('AQ', 'Antarctica'), ('AR', 'Argentina'), ('AM', 'Armenia'), ('AU', 'Australia'), ('AT', 'Austria'), ('AW', 'Aruba'), ('AZ', 'Azerbaijan'), ('BA', 'Bosnia and Herzegovina'), ('BB', 'Barbados'), ('BD', 'Bangladesh'), ('BE', 'Belgium'), ('BF', 'Burkina Faso'),
'es': u'español', 'fr': u'français', 'he': u'עברית', 'hi': u'हिन्दी', 'it': u'italiano', 'ja': u'日本語', 'ko': u'한국어', 'nl': u'Nederlands', 'ru': u'Pyccĸий', 'pt': u'português', 'tr': u'Tϋrkçe', 'zh_cn': u'简化中国' } ''' Setup redis caching connection to be used throughout the site. Credentials are set in their respective env vars. ''' REDIS = Redis(host=get_env_variable("OEC_REDIS_HOST", "localhost"), port=get_env_variable("OEC_REDIS_PORT", 6379), password=get_env_variable("OEC_REDIS_PW", None)) REDIS_CACHE = RedisCache(host=get_env_variable("OEC_REDIS_HOST", "localhost"), port=get_env_variable("OEC_REDIS_PORT", 6379), password=get_env_variable("OEC_REDIS_PW", None), default_timeout=2591999) try: REDIS.client_list() except ConnectionError: REDIS, REDIS_CACHE = [None] * 2 FACEBOOK_ID = get_env_variable("OEC_FACEBOOK_ID", 0)
from redis import Redis cache = Redis(host='192.168.0.6', port=6379, password='******') # 1.操作字符串 # cache.set('username', 'eric', ex=20) # print(cache.get('username')) # cache.delete('username') # 2.列表操作 # cache.lpush('languages', 'java') # cache.lpush('languages', 'python') # cache.rpush('languages', 'php') # print(cache.lrange('languages', 0, -1)) # # 3.集合操作 # cache.sadd('team', 'li') # cache.sadd('team', 'huang') # cache.sadd('team', 'zhang') # print(cache.smembers('team')) # 4.哈希操作 # cache.hset('website', 'baidu', 'www.baidu.com') # cache.hset('website', 'google', 'www.google.com') # print(cache.hgetall('website')) # 5.事务操作 # pip = cache.pipeline() # pip.set('username', 'eric') # pip.set('password', '1111') # pip.execute()
sleep(1) print( dumps( { "event": "PostgreSQL Connection failed, retrying...", "level": "warning", "logger": __name__, } ) ) while True: try: redis = Redis( host=CONFIG.y("redis.host"), port=6379, db=CONFIG.y("redis.message_queue_db"), password=CONFIG.y("redis.password"), ) redis.ping() break except RedisError: sleep(1) print( dumps( { "event": "Redis Connection failed, retrying...", "level": "warning", "logger": __name__, } ) )
from redis import Redis rd = Redis(host='localhost', db=1) def save_code(phone, code): rd.set(phone, code) rd.expire(phone, 120) # 120秒存活期 def get_code(phone): if rd.exists(phone): return rd.get(phone) return None
from flask import Flask from redis import Redis, RedisError import os import socket # connect to redis redis = Redis(host='redis', db=0, socket_connect_timeout=2, socket_timeout=2) app = Flask(__name__) @app.route("/") def hello(): try: visits = redis.incr("counter") except RedisError: visits = "<i>cannot connect to Redis, counter disabled</i>" html = "<h3>Hello {name}!" \ "<b>Hostname: </b> {hostname}<br/>" \ "<b>Visits:</b> {visits}" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__": app.run(host="0.0.0.0", port=80)
# coding=utf-8 # redis字符串操作 from redis import Redis DIVIDING = '-' * 30 if __name__ == '__main__': rc = Redis(host='127.0.0.1', port=6379) rc.flushdb() # 给数据库中键为name的string赋予值value print rc.set('s', 111) # 返回数据库中键为name的string的value print rc.get('s') print type(rc.get('s')) print DIVIDING # 给数据库中键为name的string赋予值value并返回上次的value print rc.getset('s', 222) print rc.get('s') print DIVIDING # 如果不存在这个键值对,则更新value,否则不变 print rc.setnx('s', 0) print rc.setnx('snx', 0) print rc.get('s') print rc.get('snx') print DIVIDING
from rq import Queue, Worker, Connection from redis import Redis import crawl_config if __name__ == '__main__': redis_conn = Redis(crawl_config.redis_server) with Connection(connection=redis_conn): q = Queue() Worker(q).work()
import time from flask import Flask from redis import Redis app = Flask(__name__) cache = Redis(host='localhost', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): cache.incr('hits') return 'This compose has been viewed {} time(s)'.format('hits')
# Copyright (C) 2016 Deloitte Argentina. # This file is part of CodexGigas - https://github.com/codexgigassys/ # See the file 'LICENSE' for copying permission. from redis import Redis from rq import Queue import sys from env import envget qfail = Queue(sys.argv[1], connection=Redis(host=envget('redis.host'))) qfail.count qfail.empty()
def __init__(self): from redis import Redis self.r = Redis(host=settings.TAGDB_REDIS_HOST,port=settings.TAGDB_REDIS_PORT,db=settings.TAGDB_REDIS_DB)
# -*- coding:utf-8 -*- from flask import Blueprint, request import json from models import * from redis import Redis """ 登录页面存在三个组件(可只完成登录,注册两个组件) """ tokens = {} r = Redis() appLogin = Blueprint("login", __name__, url_prefix='/login') @appLogin.route("/", methods=('post', 'get')) def login(): global tokens data = request.form data = list(data)[0] data = json.loads(data) print(type(data), data) acc = accountinfo.query.filter(accountinfo.company == data["company"], accountinfo.id == data["username"]).first() if acc is None: return json.dumps({"code": "404", "data": {"token": ""}}) else: print(acc.pwd) if acc.pwd != data["password"]: return json.dumps({"code": "500", "data": {"token": ""}}) print("logined!!!") # tokens[acc.id] = {"id": acc.id, "cid": acc.company, "roleid": acc.role} r.set(acc.id, json.dumps({"id": acc.id, "cid": acc.company, "roleid": acc.role}), nx=86400)
import jieba from wordcloud import WordCloud import matplotlib.pyplot as plt # 加载停用词 stopwords = set() with open('chineseStopWords.txt', encoding='gbk') as f: for line in f: # print(line.rstrip('\r\n').encode()) # bytes来观察特殊字符 stopwords.add(line.rstrip('\r\n')) stopwords.add(' ') # print(len(stopwords)) # 980 # print(stopwords) # {'公司', '另外', '一旦'...} # 读取影评 redis = Redis('192.168.2.132') items = redis.lrange('dbreview:items', 0, -1) print(type(items)) # <class 'list'> # 不使用停用词 words = {} for item in items: val = json.loads(item)['review'] for word in jieba.cut(val): words[word] = words.get(word, 0) + 1 print(len(words)) # 1558 print( sorted(words.items(), key=lambda x: x[1], reverse=True)[:10] ) # [(',', 338), ('的', 163), ('。', 120), ('了', 76), ('是', 40), ('海王', 37), ...] # 使用停用词
def __init__(self, sql_db_object): self.client = Redis(decode_responses=True) self.sql_db = sql_db_object
from django.shortcuts import render from django.http import HttpResponse from rest_framework.decorators import action from rq import Queue import pickle from redis import Redis from bs4 import BeautifulSoup import requests redisClient = Redis(host='redis') bookInfoParserQueue = Queue('bookInfoParser',connection=redisClient) generate_redis_key_for_book = lambda bookURL: 'GOODREADS_BOOKS_INFO:' + bookURL def parse_book_link_for_meta_data(bookLink): htmlString = requests.get(bookLink).content # Fetch HTML string of the book information page bsTree = BeautifulSoup(htmlString,"html.parser") # Build a searchable tree using fetched HTML # Find the required book attributes in the tree title = bsTree.find("h1", attrs={"id": "bookTitle"}).string author = bsTree.find("a", attrs={"class": "authorName"}).span.string rating = bsTree.find("span", attrs={"itemprop": "ratingValue"}).string description = ''.join(bsTree.find("div", attrs={"id": "description"}).find("span", attrs={"style": "display:none"}).stripped_strings) return dict(title=title.strip() if title else '',author=author.strip() if author else '',rating=float(rating.strip() if rating else 0),description=description) def parse_and_persist_book_info(bookUrl): redisKey = generate_redis_key_for_book(bookUrl) bookInfo = parse_book_link_for_meta_data(bookUrl) redisClient.set(redisKey,pickle.dumps(bookInfo)) def parse_goodreads_urls(request): bodyJSON = request.get_json() # Get JSON body from POST request if (isinstance(bodyJSON,list) and len(bodyJSON)): # Check whether JSON is list or not
import os import urllib.parse as urlparse from redis import Redis from rq import Worker, Queue, Connection listen = ['high', 'default', 'low'] redis_url = os.getenv('REDISTOGO_URL') if not redis_url: raise RuntimeError('Set up Redis To Go first.') urlparse.uses_netloc.append('redis') url = urlparse.urlparse(redis_url) conn = Redis(host=url.hostname, port=url.port, db=0, password=url.password) with Connection(conn): worker = Worker(map(Queue, listen)) worker.work()
# -*- coding: UTF-8 -*- import sys from redis import Redis from rq import Queue from gpusher.main import git_push q = Queue(connection=Redis()) q.enqueue(git_push, sys.argv[1])
from redis import Redis from rq import Worker, Queue, Connection listen = ['high', 'default', 'low'] redis_conn = Redis(host='redis', port=6379) if __name__ == '__main__': with Connection(redis_conn): worker = Worker(map(Queue, listen)) worker.work()
relevant = json.loads(open(args["relevant"]).read()) queryFilename = args["query"][args["query"].rfind("/") + 1:] queryRelevant = relevant[queryFilename] # load the query image and process it queryImage = cv2.imread(args["query"]) cv2.imshow("Query", imutils.resize(queryImage, width=320)) queryImage = imutils.resize(queryImage, width=320) queryImage = cv2.cvtColor(queryImage, cv2.COLOR_BGR2GRAY) # extract features from the query image and construct a bag-of-visual-word from it (_, descs) = dad.describe(queryImage) hist = bovw.describe(descs).tocoo() # connect to redis and perform the search redisDB = Redis(host="localhost", port=6379, db=0) searcher = Searcher(redisDB, args["bovw_db"], args["features_db"], idf=idf, distanceMetric=distanceMetric) sr = searcher.search(hist, numResults=20) print("[INFO] search took: {:.2f}s".format(sr.search_time)) # initialize the results montage montage = ResultsMontage((240, 320), 5, 20) # loop over the individual results for (i, (score, resultID, resultsIdx)) in enumerate(sr.results): # load the result image and display it print("[RESULT] {result_num}. {result} - {score:.2f}".format(
from flask import Flask from redis import Redis, RedisError import os import socket # Connect to Redis redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2) app = Flask(__name__) @app.route("/") def hello(): try: visits = redis.incr("counter") except RedisError: visits = "<i>cant connect to Redis, counter disabled</i>" html = "<h3>HEllo (name)! </h3>" \ "<b> Hostname: </b> (hostname)<br/>" \ "<b> Visits :</b> (visits)" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__": app.run(host='0.0.0.0', port=80)
def get_redis_connection(): return Redis(db=settings.REDIS_DATABASE_DB, host=settings.REDIS_DATABASE_HOST, port=settings.REDIS_DATABASE_PORT, password=settings.REDIS_DATABASE_PASSWORD)