#347 容错、高可用、灾备

2019-11-19

阮一峰的博文(容错,高可用和灾备)中说:

  • 容错:发生故障时,如何让系统继续运行。
    飞机的四个引擎坏了一个还能继续飞行,汽车的四个轮子坏了一个也能将就驾驶。
  • 高可用:系统中断时,如何尽快恢复。
    汽车的备胎,用于快速恢复正常驾驶(允许短暂的业务中断)。
  • 灾备:系统毁灭时,如何抢救数据。
    飞机的弹射装置,保证最核心的“资产” —— 驾驶员能够存活。

#346 开发者 Cheatsheets

2019-11-18

Linux

  • https://linuxopsys.com/topics/linux-commands-cheat-sheet
  • grep
  • awk

Bash & ShellScripting

  • https://oit.ua.edu/wp-content/uploads/2020/12/Linux_bash_cheat_sheet-1.pdf
  • http://www.cheat-sheets.org/saved-copy/shellscripcheatsheet.pdf

Linux Network

  • https://opensource.com/downloads/cheat-sheet-networking
    • https://opensource.com/sites/default/files/gated-content/osdc_cheatsheet-networking-2021.4.8.pdf
  • Hacking Tools Cheat Sheet

Windows

cmd

  • http://www1.cs.columbia.edu/~sedwards/classes/2017/1102-spring/Command%20Prompt%20Cheatsheet.pdf
  • https://phoenixnap.com/kb/wp-content/uploads/2023/01/windows-cmd-commands-cheat-sheet-pdf.pdf

PowerShell

  • http://ramblingcookiemonster.github.io/images/Cheat-Sheets/powershell-basic-cheat-sheet2.pdf

Vim

  • https://phoenixnap.com/kb/vim-commands-cheat-sheet
  • https://www.cs.cmu.edu/~15131/f17/topics/vim/vim-cheatsheet.pdf
  • https://github.com/dennyzhang/cheatsheet-vim-A4
    • https://github.com/dennyzhang/cheatsheet-vim-A4/blob/master/cheatsheet-vim-A4.pdf
  • https://gitee.com/chloneda/vim-cheatsheet

Python

  • https://hackr.io/blog/python-cheat-sheet
  • https://www.pythoncheatsheet.org/
  • https://static.realpython.com/python-cheat-sheet.pdf
  • https://static.realpython.com/python_cheat_sheet_v1.pdf
  • https://courses.cs.washington.edu/courses/cse163/22wi/resources/python-cheat-sheet.pdf

Django

  • http://cheat-sheets.org/saved-copy/django_reference_sheet.pdf
  • https://cheatography.com/ogr/cheat-sheets/django/
  • https://edu.anarcho-copy.org/Programming%20Languages/Python/Python%20CheatSheet/beginners_python_cheat_sheet_pcc_django.pdf
  • https://www.riptutorial.com/Download/django-rest-framework.pdf
  • https://books.agiliq.com/_/downloads/django-api-polls-tutorial/en/latest/pdf/

NumPy

  • http://www.cheat-sheets.org/saved-copy/numpy-cheat-sheet.20210604.pdf

Golang

  • http://www.cheat-sheets.org/saved-copy/go-lang-cheat-sheet-master.20181212/golang_refcard.pdf
  • https://golang.sk/images/blog/cheatsheets/go-cheat-sheet.pdf
  • https://github.com/fedir/go-tooling-cheat-sheet
    • https://raw.githubusercontent.com/fedir/go-tooling-cheat-sheet/master/go-tooling-cheat-sheet.pdf

C/C++

  • http://dcjtech.info/wp-content/uploads/2017/09/C-Programming.pdf
  • https://cheatography.com/ashlyn-black/cheat-sheets/c-reference/
  • https://opensource.com/sites/default/files/gated-content/cheat_sheet_c.pdf
  • https://courses.cs.washington.edu/courses/cse351/14sp/sections/1/Cheatsheet-c.pdf
  • https://web.pa.msu.edu/people/duxbury/courses/phy480/Cpp_refcard.pdf
  • https://cheatography.com/technecure/cheat-sheets/c-cheatsheet/
  • https://cppcheatsheet.readthedocs.io/_/downloads/en/latest/pdf/
  • https://cheatography.com/benjy/cheat-sheets/c/

Java

  • https://computinglearner.com/wp-content/uploads/2021/09/Java-Cheat-Sheet.pdf

PHP

  • https://websitesetup.org/wp-content/uploads/2020/09/PHP-Cheat-Sheet.pdf

Web

HTML

  • https://html.com/wp-content/uploads/html-cheat-sheet.pdf

CSS

  • https://websitesetup.org/wp-content/uploads/2019/11/wsu-css-cheat-sheet-gdocs.pdf

JavaScript

  • https://websitesetup.org/wp-content/uploads/2020/09/Javascript-Cheat-Sheet.pdf

jQuery

  • https://websitesetup.org/wp-content/uploads/2017/01/wsu-jquery-cheat-sheet.pdf
  • https://cheat-sheets.org/saved-copy/wsu-jquery-cheat-sheet.pdf

Vue

  • https://learnvue.co/LearnVue-Vue-3-Cheatsheet.pdf
  • https://vuejs-tips.github.io/cheatsheet/vuejs-cheatsheet.pdf

Redis

  • http://stephendavies.org/cpsc350/redisCheatSheet.pdf

MySQL

  • https://phoenixnap.com/kb/wp-content/uploads/2021/04/MySQL-Commands-Cheat-Sheet-by-PhoenixNAP.pdf

MongoDB

  • https://cheatography.com/isaeus/cheat-sheets/mongodb/
  • http://stephendavies.org/cpsc350/mongoCheatSheet.pdf
  • https://www.goalkicker.com/MongoDBBook/MongoDBNotesForProfessionals.pdf

RabbitMQ

  • https://pika.readthedocs.io/_/downloads/en/stable/pdf/

工具

Git

  • https://education.github.com/git-cheat-sheet-education.pdf

Excel

  • https://download.microsoft.com/download/5/4/c/54cd97c1-0213-46a7-b659-95f1c9145f42/Excel_shortcuts_cheat_sheet.pdf

PyTorch

  • https://www.mad.tf.fau.de/files/2019/07/pytorch-cheatsheet-en.pdf

#345 Python sys.audit

2019-11-06

在学习 subprocess 模块的时候看到代码中有一行:

sys.audit("subprocess.Popen", executable, args, cwd, env)

才了解到 Python 3.8 中新加了这样一个审计功能。

作用

提供一种机制,对 Python 程序执行过程中的部分关键操作进行干预,比如记录事件、引发异常、中止操作或完全终止进程。

部分关键操作发生的时候,Python 会调用我们定义的钩子方法。

注意:官方提示,恶意代码可以轻易绕过这些钩子,不要指望审计钩子能用来创建一个安全的 sandbox。
注意:sys.addaudithook 方法的调用会触发一个同名的事件。如果现有的钩子方法抛出 Runtime 异常,则不会添加这个钩子。

支持的审计事件

CPython 定义了一些事件:Audit events table,比如:

  • import
  • exec
  • compile
  • os.exec
  • os.listdir
  • os.rmdir
  • os.spawn
  • os.walk
  • os.system
  • subprocess.Popen

使用

sys.audit 方法是官方定义和执行的,Python 运行时负责抛出审计事件,我们程序只管使用系统方法 sys.addaudithook(hook) 来添加审计钩子处理这些事件。

import sys

def hook(event, args):
    print('%s: %r' % (event, args))

sys.addaudithook(hook)

import math
num = math.ceil(100 / 3)
print(num)

#344 电磁波与可见光

2019-11-03

光、电、磁三者好像完全没有关系。但是近代的一些科学研究表明它们其实是一种东西 —— 电磁波。

1873 年,42 岁的麦克斯韦出版了《论电和磁》,预言了电磁波的存在,同时认为光是电磁波的一种。
PS: 1879 年,48 岁的麦克斯韦因病去世,同年,爱因斯坦诞生。

1905 年,奇迹之年,最著名的“民科” —— 26 岁的专利局职员爱因斯坦发表了一篇论文《On the Electrodynamics of Moving Bodies》,中文翻译为“论动体的电动力学”。论文中指出,电和磁是在不同参考系中观察到的同一种现象。

最后,科学家发现 光、电、磁 的本质,和微观粒子,和量子力学有很大的关系。

我敢说,对于 光、电、磁 三者本质的认知,目前,最顶尖的科学家也不敢打包票。

什么是光?

我们所说的光,又称之为可见光,与紫外线、红外线、X 光等区分开。
实质上来讲,可见光只是电磁波谱上一小段频谱,我们的肉眼对这一段比较敏感而已。
PS: 不同动物对不同频段的电磁波敏感度不同,所以其他动物(比如昆虫)看到的世界和我们很大有很大差异。

img

高中物理就告诉我们:

  1. 光是一种电磁波,波长在 500nm 左右。
  2. 光是由光子组成的粒子流。
  3. 光同时具有波的特性、粒子的特性,这叫做波粒二象性。

电和磁

我们初中就开始学习电,老师教我们,电是由于电子运动产生的物理现象。通过施加电压,电子就会沿着电场运动。

磁则来自磁铁,或者电流。

电磁波

电磁波是指同相振荡且互相垂直的电场与磁场,在空间中以波的形式传递能量和动量,其传播方向垂直于电场与磁场的振荡方向。

  1. 电场和磁场相互垂直,同生同灭。
  2. 电场和磁场同相振荡。
  3. 电磁波可以传输能量和动量。

左手定律,右手定律,相信很多朋友记忆深刻。

电磁波的频率

img

频率由低到高(波长由长到短):

  • 无线电波: 300GHz 1mm ~ 3KHz 100km
  • 微波: 300MHz ~ 300GHz 1m
  • 我们的微波炉 2.45GHz。
  • 红外线 760nm ~ 1000nm (1mm)
  • 可见光 380nm ~ 780nm
  • 紫外线 10nm ~ 400nm
  • X 射线 0.01nm ~ 10nm
  • 伽马射线(γ射线): 小于 0.01nm
  • 用于医疗:放射治疗(放疗)、伽马刀
  • 超新星爆发

为什么光能透过空气、水、玻璃,不能透过墙壁?

参考资料与拓展阅读

#343 尝试 K8S(Kubernetes)

2019-10-15

安装

安装 minikube

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && sudo install minikube-linux-amd64 /usr/local/bin/minikube
$ minikube version
minikube version: v1.0.0

安装 kubectl

https://packages.cloud.google.com/apt/doc/apt-key.gpg
https://apt.kubernetes.io/

sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl kubelet kubeadm

Docker 的配置文件:~/.docker/config.json。至少,docker login 之后会生成一个。

~/.minikube/

apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

参考资料与拓展阅读

#340 Django 3.0 发布,开始支持异步功能

2019-09-16

Django 项目组试图在保持向后兼容的基础之上,对阻塞部分进行改造,使之支持异步(通过装饰器的方式)。
包括 Session、Auth、ORM 与 Handlers 等。

ASGI 模式将 Django 作为原生异步应用程序运行,原有的 WSGI 模式将围绕每个 Django 调用运行单个事件循环,以使异步处理层与同步服务器兼容。

在这个改造的过程中,每个特性都会经历以下三个实现阶段:

  • Sync-only,只支持同步,也就是当前的情况
  • Sync-native,原生同步,同时带有异步封装器
  • Async-native,原生异步,同时带同步封装器

Django 3.0 开始提供运行 ASGI 应用支持,让 Django 逐渐具备异步功能。做了这一改动后,Django 现在会感知到异步事件循环,并将阻止从异步上下文调用标记为 “异步不安全” 的代码(例如 ORM 操作),如果开发者之前使用的是异步代码,则可能会触发。如果看到 SynchronousOnlyOperation 错误,可以仔细检查代码并将数据库操作移到同步子线程中。

其它方面,Django 现在支持 MariaDB 10.1 及更高版本;新的 ExclusionConstraint 类可以在 PostgreSQL 上添加排除约束;输出 BooleanField 的表达式现在可以直接在 QuerySet 过滤器中使用,而无需先注解然后对注解进行过滤;自定义枚举类型 TextChoices、IntegerChoices 和 Choices 现在可用作定义 Field.choices 的方法。

需要特别注意的是:自从 2.2 之后,Django 将不再支持 Python 3.5。

更新说明:https://docs.djangoproject.com/en/dev/releases/3.0

#339 Django 版本历史

2019-09-16
Ver Date Notes
1.0 2008-09-03  
1.1 2009-07-29  
1.2 2010-05-17  
1.3 2011-03-23  
1.4 2012-03-23 LTS
1.5 2013-02-26  
1.6 2013-11-06  
1.7 2014-09-02  
1.8 2015-04-01 LTS 2018-04-01
1.9 2015-12-01  
1.10 2016-08-01  
1.11 2017-04-04 LTS 2020-04-01
2.0 2017-12-02  
2.1 2018-08-01  
2.2 2019-04-01 LTS 2022-04-01
3.0 2019-12-02  
3.1 2020-08-04  
3.2 2021-04-06 LTS 2024-04-01
4.0 2021-12-07  
4.1 2022-08  
4.2 2023-04 LTS 2026-04-01
5.0 2023-12