redis 4.2.0 merged aioredis-迁移小计

背景

Add Async Support #1899

https://github.com/redis/redis-py/pull/1899

Version 4.2.0-rc1

https://github.com/redis/redis-py/releases/tag/v4.2.0rc1



迁移

官方做法

https://github.com/aio-libs/aioredis-py

Aioredis is now in redis-py 4.2.0rc1+
To install, just do pip install redis>=4.2.0rc1. The code is almost the exact same. You will just need to import like so:

from redis import asyncio as aioredis

This way you don’t have to change all your code, just the imports.

我的做法

统一使用redis库,去掉aioredis库及概念,适用于仅用异步方式使用redis场景。

aioredis【迁移前】
1
2
3
4
5
6
7
8
# 引入库
import aioredis

from aioredis.exceptions import DataError, ResponseError


pool = aioredis.ConnectionPool.from_url(url, decode_responses=True)
connection = aioredis.Redis(connection_pool=pool)
redis 【迁移后】
1
2
3
4
5
6
7
import redis.asyncio as redis  

from redis.exceptions import DataError, ResponseError


pool = redis.ConnectionPool.from_url(url, decode_responses=True)
connection = redis.Redis(connection_pool=pool)

PSQL序

切换到postgres用户进入数据库控制台

hightlight:true
1
sudo -u postgres psql

创建新数据库

hightlight:true
1
create database newdbname [owner username]

赋予用户数据库所有权限

hightlight:true
1
grant all privileges on database dbname to username

更改数据库owner

hightlight:true
1
alter database dbname owner to username

查看所有数据库列表

hightlight:true
1
\l

连接数据库

hightlight:true
1
\c dbname 

显示所有的schema

hightlight:true
1
\dn

显示所有的用户

hightlight:true
1
\du

显示表的权限分配情况

hightlight:true
1
\dp

显示当前的模式

hightlight:true
1
show search_path

更改模式

hightlight:true
1
set search_path to myschema

已列的形式展示或取消

hightlight:true
1
\x 

查看所有表

hightlight:true
1
\dt

显示执行时间

hightlight:true
1
\timing on     

关闭显示执行时间

hightlight:true
1
\timing off

禁用全表扫描

hightlight:true
1
2
alter role rolename set enable_seqscan = off;
set enable_seqscan = off;

PSQL COMMANDS

General

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\copyright             
# show PostgreSQL usage and distribution terms

\errverbose
# show most recent error message at maximum verbosity

\g [FILE] or ;
# execute query (and send results to file or |pipe)

\gexec
# execute query, then execute each value in its result

\gset [PREFIX]
# execute query and store results in psql variables

\q
# quit psql

\crosstabview [COLUMNS]
# execute query and display results in crosstab

\watch [SEC]
# execute query every SEC seconds

Help

1
2
3
4
5
6
7
8
9
10
11
\? [commands]          
# show help on backslash commands

\? options
# show help on psql command-line options

\? variables
# show help on special variables

\h [NAME]
# help on syntax of SQL commands, * for all commands

Query Buffer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\e [FILE] [LINE]       
# edit the query buffer (or file) with external editor

\ef [FUNCNAME [LINE]]
# edit function definition with external editor

\ev [VIEWNAME [LINE]]
# edit view definition with external editor

\p
# show the contents of the query buffer

\r
# reset (clear) the query buffer

\s [FILE]
# display history or save it to file

\w FILE
# write query buffer to file

Input/Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\copy ...              
# perform SQL COPY with data stream to the client host

\echo [STRING]
# write string to standard output

\i FILE
# execute commands from file

\ir FILE
# as \i, but relative to location of current script

\o [FILE]
# send all query results to file or |pipe

\qecho [STRING]
# write string to query output stream (see \o)

Informational

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(options: S = show system objects, + = additional detail)

\d[S+]
# list tables, views, and sequences

\d[S+] NAME
# describe table, view, sequence, or index

\da[S] [PATTERN]
# list aggregates

\dA[+] [PATTERN]
# list access methods

\db[+] [PATTERN]
# list tablespaces

\dc[S+] [PATTERN]
# list conversions

\dC[+] [PATTERN]
# list casts

\dd[S] [PATTERN]
# show object descriptions not displayed elsewhere

\ddp [PATTERN]
# list default privileges

\dD[S+] [PATTERN]
# list domains

\det[+] [PATTERN]
# list foreign tables

\des[+] [PATTERN]
# list foreign servers

\deu[+] [PATTERN]
# list user mappings

\dew[+] [PATTERN]
# list foreign-data wrappers

\df[antw][S+] [PATRN]
# list [only agg/normal/trigger/window] functions

\dF[+] [PATTERN]
# list text search configurations

\dFd[+] [PATTERN]
# list text search dictionaries

\dFp[+] [PATTERN]
# list text search parsers

\dFt[+] [PATTERN]
# list text search templates

\dg[S+] [PATTERN]
# list roles

\di[S+] [PATTERN]
# list indexes

\dl
# list large objects, same as \lo_list

\dL[S+] [PATTERN]
# list procedural languages

\dm[S+] [PATTERN]
# list materialized views

\dn[S+] [PATTERN]
# list schemas

\do[S] [PATTERN]
# list operators

\dO[S+] [PATTERN]
# list collations

\dp [PATTERN]
# list table, view, and sequence access privileges

\drds [PATRN1 [PATRN2]]
# list per-database role settings

\ds[S+] [PATTERN]
# list sequences

\dt[S+] [PATTERN]
# list tables

\dT[S+] [PATTERN]
# list data types

\du[S+] [PATTERN]
# list roles

\dv[S+] [PATTERN]
# list views

\dE[S+] [PATTERN]
# list foreign tables

\dx[+] [PATTERN]
# list extensions

\dy [PATTERN]
# list event triggers

\l[+] [PATTERN]
# list databases

\sf[+] FUNCNAME
# show a function's definition

\sv[+] VIEWNAME
# show a view's definition

\z [PATTERN]
# same as \dp

Formatting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
\a                     
# toggle between unaligned and aligned output mode

\C [STRING]
# set table title, or unset if none

\f [STRING]
# show or set field separator for unaligned query output

\H
# toggle HTML output mode (currently off)

\pset [NAME [VALUE]]
# set table output option
# (NAME := {border|columns|expanded|fieldsep|fieldsep_zero|
# footer|format|linestyle|null|numericlocale|pager|
# pager_min_lines|recordsep|recordsep_zero|tableattr|title|
# tuples_only|unicode_border_linestyle|
# unicode_column_linestyle|unicode_header_linestyle})

\t [on|off]
# show only rows (currently off)

\T [STRING]
# set HTML <table> tag attributes, or unset if none

\x [on|off|auto]
# toggle expanded output (currently off)

Connection

1
2
3
4
5
6
7
8
9
10
11
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
# connect to new database (currently "crm_online_test")

\encoding [ENCODING]
# show or set client encoding

\password [USERNAME]
# securely change the password for a user

\conninfo
# display information about current connection

Operating System

1
2
3
4
5
6
7
8
9
10
11
\cd [DIR]              
# change the current working directory

\setenv NAME [VALUE]
# set or unset environment variable

\timing [on|off]
# toggle timing of commands (currently on)

\! [COMMAND]
# execute command in shell or start interactive shell

Variables

1
2
3
4
5
6
7
8
\prompt [TEXT] NAME    
# prompt user to set internal variable

\set [NAME [VALUE]]
# set internal variable, or list all if no parameters

\unset NAME
# unset (delete) internal variable

Large Objects

1
2
3
4
5
6
7
8
\lo_export LOBOID FILE

\lo_import FILE [COMMENT]

\lo_list

\lo_unlink LOBOID
# large object operations

SQL Commands

全览图

命令表

ABORT COMMENT DECLARE EXECUTE
ALTER AGGREGATE COMMIT DELETE EXPLAIN
ALTER COLLATION COMMIT PREPARED DISCARD FETCH
ALTER CONVERSION COPY DO GRANT
ALTER DATABASE CREATE ACCESS METHOD DROP ACCESS METHOD IMPORT FOREIGN SCHEMA
ALTER DEFAULT PRIVILEGES CREATE AGGREGATE DROP AGGREGATE INSERT
ALTER DOMAIN CREATE CAST DROP CAST LISTEN
ALTER EVENT TRIGGER CREATE COLLATION DROP COLLATION LOAD
ALTER EXTENSION CREATE CONVERSION DROP CONVERSION LOCK
ALTER FOREIGN DATA WRAPPER CREATE DATABASE DROP DATABASE MOVE
ALTER FOREIGN TABLE CREATE DOMAIN DROP DOMAIN NOTIFY
ALTER FUNCTION CREATE EVENT TRIGGER DROP EVENT TRIGGER PREPARE
ALTER GROUP CREATE EXTENSION DROP EXTENSION PREPARE TRANSACTION
ALTER INDEX CREATE FOREIGN DATA WRAPPER DROP FOREIGN DATA WRAPPER REASSIGN OWNED
ALTER LANGUAGE CREATE FOREIGN TABLE DROP FOREIGN TABLE REFRESH MATERIALIZED VIEW
ALTER LARGE OBJECT CREATE FUNCTION DROP FUNCTION REINDEX
ALTER MATERIALIZED VIEW CREATE GROUP DROP GROUP RELEASE SAVEPOINT
ALTER OPERATOR CREATE INDEX DROP INDEX RESET
ALTER OPERATOR CLASS CREATE LANGUAGE DROP LANGUAGE REVOKE
ALTER OPERATOR FAMILY CREATE MATERIALIZED VIEW DROP MATERIALIZED VIEW ROLLBACK
ALTER POLICY CREATE OPERATOR DROP OPERATOR ROLLBACK PREPARED
ALTER ROLE CREATE OPERATOR CLASS DROP OPERATOR CLASS ROLLBACK TO SAVEPOINT
ALTER RULE CREATE OPERATOR FAMILY DROP OPERATOR FAMILY SAVEPOINT
ALTER SCHEMA CREATE POLICY DROP OWNED SECURITY LABEL
ALTER SEQUENCE CREATE ROLE DROP POLICY SELECT
ALTER SERVER CREATE RULE DROP ROLE SELECT INTO
ALTER SYSTEM CREATE SCHEMA DROP RULE SET
ALTER TABLE CREATE SEQUENCE DROP SCHEMA SET CONSTRAINTS
ALTER TABLESPACE CREATE SERVER DROP SEQUENCE SET ROLE
ALTER TEXT SEARCH CONFIGURATION CREATE TABLE DROP SERVER SET SESSION AUTHORIZATION
ALTER TEXT SEARCH DICTIONARY CREATE TABLE AS DROP TABLE SET TRANSACTION
ALTER TEXT SEARCH PARSER CREATE TABLESPACE DROP TABLESPACE SHOW
ALTER TEXT SEARCH TEMPLATE CREATE TEXT SEARCH CONFIGURATION DROP TEXT SEARCH CONFIGURATION START TRANSACTION
ALTER TRIGGER CREATE TEXT SEARCH DICTIONARY DROP TEXT SEARCH DICTIONARY TABLE
ALTER TYPE CREATE TEXT SEARCH PARSER DROP TEXT SEARCH PARSER TRUNCATE
ALTER USER CREATE TEXT SEARCH TEMPLATE DROP TEXT SEARCH TEMPLATE UNLISTEN
ALTER USER MAPPING CREATE TRANSFORM DROP TRANSFORM UPDATE
ALTER VIEW CREATE TRIGGER DROP TRIGGER VACUUM
ANALYZE CREATE TYPE DROP TYPE VALUES
BEGIN CREATE USER DROP USER WITH
CHECKPOINT CREATE USER MAPPING DROP USER MAPPING
CLOSE CREATE VIEW DROP VIEW
CLUSTER DEALLOCATE END

Lantern

操作界面

每个月开放免费的高速流量有500M

足够平常的代码更新下载,Facebook、Twitter、Instagram的使用,如果大量访问YouTube 估计就得升级专业版了

下载地址

https://github.com/getlantern/download

结合自己的系统下载对应的版本进行安装即可

设置管理

取消勾选开机自动启动

有的时候免费线路信号不好,连接不上,会影响原本网络的正常使用,所以在需要用到的时候再开启蓝灯,点击连接。

终端nano实现Mac支持ntfs磁盘读写

无需安装第三方软件(如:Paragon NTFS、Mounty)实现Mac支持ntfs磁盘读写

之前已经实现了cc盘的可读写挂载,现在进行另外三个盘的修改

先获取三个磁盘的UUID

关于本机

选取 系统报告

记下磁盘对应的UUID

在nano中添加设置

在终端输入sudo nano /etc/fstab 进入编辑界面

现在你看到了一个编辑界面

分别输入三个磁盘的设置

  • UUID=硬盘对应的UUID号码 none ntfs rw,auto,nobrowse
  • 换行回车

Ctrl+X保存

确认保存操作 敲击Y

回车 退出当前编辑窗口

设置成功 查看磁盘读写效果

推出磁盘

重新插入,发现Finder中没有显示这几个盘

Finder左上角工具栏中选择 前往 -> 前往文件夹

输入/Volumes/

出现所有的磁盘信息

将磁盘拉到左边边栏列表中 设置磁盘快捷访问

查看效果 已经可以进行写操作了

Supervisor配置管理Python项目

supervisord.conf

hightlight:true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
chmod=0700 ; socket file mode (default 0700)

[inet_http_server] ; inet (TCP) server disabled by default
port=*:8001
username=your_username ; (default is no username (open server))
password=your_password ; (default is no password (open server))

[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
environment=MONGO_URL="mongodb://reminder:geN-aud-veff@127.0.0.1:27017/reminder"

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket

[program:program_name]
directory=/program_dir
command=uwsgi -w program_name_product --ini program_name.ini --uid role
startsecs=2
numproces=2
stopasgroup=true
autostart=false
autorestart=true
stderr_logfile=/tmp/log/program_name_error.log
stdout_logfile=/tmp/log/program_name_access.log

[program:program_name_n]
directory=/program_n_dir
command=command_n
startsecs=2
numproces=2
stopasgroup=true
autostart=false
autorestart=true
stderr_logfile=/tmp/log/program_name_n_error.log
stdout_logfile=/tmp/log/program_name_n_access.log

进程管理控制台常用命令

hightlight:true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- supervisorctl # 进入控制台

- start all # 启动配置文件中的所有进程
- stop all # 停止配置文件中的所有进程
- restart all # 重启配置文件中的所有进程

- start program_name # 启动进程
- stop program_name # 停止进程
- restart program_name # 重启进程

- start groupworker: # 启动所有属于名为 groupworker 这个分组的进程
- stop groupworker: # 结束所有属于名为 groupworker 这个分组的进程
- restart groupworker: # 重启所有属于名为 groupworker 这个分组的进程

- start groupworker:name1 # 启动 groupworker:name1 这个进程
- stop groupworker:name1 # 结束 groupworker:name1 这个进程
- restart groupworker:name1 # 重启 groupworker:name1 这个进程

- reload # 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程

- update # 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启遇到问题
注:start、restartUnlinking stale socket /tmp/supervisor.sock、stop 都不会载入最新的配置文件

- exit # 退出supervisor控制台

Nginx+uWSGI部署多个Django项目

uWSGI配置

project_1 的 uWSGI 的配置文件 project_1.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[uwsgi]

chdir = /project_1/

module = project_1.wsgi

master = true

processes = 10

socket = /project_1/project_1.sock

chmod-socket = 666
chown-socket = role_1:role_1

enable-threads=true

die-on-term = true

project_n 的 uWSGI 的配置文件 project_n.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[uwsgi]

chdir = /project_n/

module = project_n.wsgi

master = true

processes = 10

socket = /project_n/project_n.sock

chmod-socket = 666
chown-socket = role_n:role_n

enable-threads=true

die-on-term = true

Nginx配置

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
server {
listen 80;

root /usr/share/nginx/www/public;
index index.php index.html index.htm;

server_name 127.0.0.1 x.x.x.x;
include blocksip.conf;
location / {
try_files $uri $uri/ /index.html;
}

error_page 404 /404.html;

location /project_1 {
include uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass unix:/project_1/project_1.sock;
uwsgi_param SCRIPT_NAME /project_1;
uwsgi_param UWSGI_SCRIPT project_1.wsgi;
uwsgi_modifier1 30;
uwsgi_read_timeout 1800s;
uwsgi_send_timeout 1800s;
}

location /project_1/media/ {
alias /usr/share/nginx/www/django_static/project_1/media/;
}

location /project_1/static/ {
alias /usr/share/nginx/www/django_static/project_1/;
}

location /apk/ {
alias /usr/share/nginx/www/django_static/project_1/apk/online/;
}

location /project_n {
include uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass unix:/project_n/project_n.sock;
uwsgi_param SCRIPT_NAME /project_n;
uwsgi_param UWSGI_SCRIPT project_n.wsgi;
uwsgi_modifier1 30;
uwsgi_read_timeout 1800s;
uwsgi_send_timeout 1800s;
}

location /project_n/media/ {
alias /usr/share/nginx/www/django_static/project_n/media/;
}

location /project_n/static/ {
alias /usr/share/nginx/www/django_static/project_n/;
}

location /apk_project_n/ {
alias /usr/share/nginx/www/django_static/project_n/apk/online/;
}
}

django后台运行

生效命令

1
2
3
4
5
nohup python manage.py runserver &  #后台运行django server

exit #退出控制台

tail -f nohup.out #监控运行过程中的输出

命令解释

1
2
3
4
5
nohup  # (no hang up) 不挂断地运行命令

& # 在后台运行命令

nohup.out # 会自动在当前目录创建该文件,记录原控制台输出内容