首先我们需要明确我们什么时候需要用到数据库:

  1. 当缓存并不能解决你的问题,比如写操作,事务操作
  2. 缓存的创建或过期需要通过数据库。

其次,我们可能需要一个专业的工具来指导我们优化:
mysqlreport
这是作为一个Mysql第三方的状态报告工具,其实就是将一下两行命令所获得的数据以一种更加人性化的方法呈现到我们眼前:

1
2
mysql> show status;
mysql> show engine innodb status;

你完全可以在自己电脑上先使用这个工具,这时候我们将逐条讲解工具为我们呈现的数据

Read more »

之所以会有这篇博客其主要原因是因为最近需要写一些脚本用于定时运行,而我个人对于Shell的crontab又是处于看了就忘,忘了就不想看的阶段,Python在我目前看来是最适合替换掉Shell脚本的手段,其中自然也有大量定时运行的技术手段,这篇对于三种可使用不同方法执行定时任务的库做个介绍,仅仅只是简单介绍,如果需要详细了解恐怕还是看其官方文档来得更加适合

Read more »

问题

  1. 同步与异步,阻塞与非阻塞有什么区别,举例说明
  2. 什么是多路复用

问题引入

这是我们通常所写的程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def normal():
for i in range(10):
print(i)

if __name__ == '__main__':
print('begin')
normal()
print('end')

#result
begin
0
1
2
3
4
5
6
7
8
9
end

通常我们的输入需要等到上条输入结束之后才能进行,这也许是我们通常最想要得到的结果,但是有某些时候如果我们所需要的结果没有前后文影响的情况下,我们可能更希望它不是这么死板地来执行的,而是异步执行,最典型的一个例子就是访问网站,网站在进行渲染的时候用到了大量的IO操作,而其他用户不可能等到一个用户渲染完之后在进行渲染,否则会造成比较差的用户体验。

事实上,所有的IO操作(如数据库查询,读写文件等)都回造成阻塞,它们都会让我们无法利用到IO执行这一期间的计算机资源。

为了解决这个问题,计算机引入了一些IO模式的区别。

Read more »

跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF 或者 XSRF, 是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法。

Read more »

首先我们看看 ab -h 命令提供的信息:

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
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform # 请求数
-c concurrency Number of multiple requests to make at a time # 并发请求数
-t timelimit Seconds to max. to spend on benchmarking # 测试所进行的最大秒数。
This implies -n 50000 # 其内部隐含值是-n 50000
-s timeout Seconds to max. wait for each response # 响应等待超时时间
Default is 30 seconds # 默认是 30 秒
-b windowsize Size of TCP send/receive buffer, in bytes # TCP窗口大小
-B address Address to bind to when making outgoing connections # 代理地址?
-p postfile File containing data to POST. Remember also to set -T # post请求包含的文件,也可以使用 -T
-u putfile File containing data to PUT. Remember also to set -T # put求包含的文件,也可以使用 -T
-T content-type Content-type header to use for POST/PUT data, eg. # post / put 请求时候 content-type 请求头
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print # 是否需要打印故障日志
-w Print out results in HTML tables # 以HTML表的格式输出结果
-i Use HEAD instead of GET # 使用 HEAD 方法代替 GET 方法
-x attributes String to insert as table attributes # 设置<table>属性的字符串。
-y attributes String to insert as tr attributes # 设置<tr>属性的字符串。
-z attributes String to insert as td or th attributes # 设置<td>属性的字符串。
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable) # 对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip' #对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")。
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes # 对服务器提供BASIC认证信任。
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes # 对一个中转代理提供BASIC认证信任。
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use # 对请求使用代理服务器。
-V Print version number and exit # 显示版本号
-k Use HTTP KeepAlive feature # 启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。
-d Do not show percentiles served table. # 不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)。
-S Do not show confidence estimators and warnings. # 不显示估计配置与警告
-q Do not show progress when doing more than 150 requests # 如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。
-l Accept variable document length (use this for dynamic pages) # 接受可变长度的文档
-g filename Output collected data to gnuplot format file. # 通过 gnuplot 文件格式收集输出数据
-e filename Output CSV file with percentages served # 产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比'gnuplot'格式更有用。
-r Don't exit on socket receive errors. # 当 socket 返回错误时不退出
-m method Method name # 请求方法名称
-h Display usage information (this message) # 显示帮助信息
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers) # 指定 SSL/TLS 加密方式
-f protocol Specify SSL/TLS protocol # 指定 SSL/TLS 协议
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

接下来我们使用 http://www.douban.com/ 作为测试网站

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
➜  ~ ab -n 100 -c 100 http://www.douban.com/
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.douban.com (be patient).....done


Server Software: dae # 测试服务器软件名称
Server Hostname: www.douban.com # 测试网址Host
Server Port: 80 # 测试端口

Document Path: / # 测试Path
Document Length: 160 bytes # 返回的报文大小

Concurrency Level: 100 # 标识并发的用户数
Time taken for tests: 0.284 seconds # 执行完所有的请求所花费的时间
Complete requests: 100 # 完成请求数
Failed requests: 0 # 失败请求数
Non-2xx responses: 100 # 返回响应非2xx数
Total transferred: 30100 bytes # 整个过程中的网络传输量
HTML transferred: 16000 bytes # 整个过程中的HTML大小
Requests per second: 352.02 [#/sec] (mean) # 吞吐率,表示每秒处理的请求数,mean标识平均值
Time per request: 284.075 [ms] (mean) # 每个用户等待时间,mean标识平均值
Time per request: 2.841 [ms] (mean, across all concurrent requests) #服务器平均请求处理的时间.
Transfer rate: 103.47 [Kbytes/sec] received # 这些请求在单位内,从服务器获取的数据长度.

Connection Times (ms) # 网络上消耗的时间的分解
min mean[+/-sd] median max
Connect: 26 37 7.9 35 63
Processing: 44 130 76.7 88 239
Waiting: 44 129 76.5 88 239
Total: 70 167 77.5 133 277

Percentage of the requests served within a certain time (ms) # 整个场景中所有请求的响应情况。
50% 133
66% 240
75% 271
80% 272
90% 275
95% 277
98% 277
99% 277
100% 277 (longest request)

前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号。本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几个小时的闲暇时间深入理解了下GIL,并归纳成此文,也希望读者能通过次本文更好且客观的理解GIL。

文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部
作者:卢钧轶(cenalulu)
本文原文地址:http://cenalulu.github.io/python/gil-in-python/

GIL是什么

首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念。就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码。有名的编译器例如GCC,INTEL C++,Visual C++等。Python也一样,同样一段代码可以通过CPython,PyPy,Psyco等不同的Python执行环境来执行。像其中的JPython就没有GIL。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python,也就想当然的把GIL归结为Python语言的缺陷。所以这里要先明确一点:GIL并不是Python的特性,Python完全可以不依赖于GIL

那么CPython实现中的GIL又是什么呢?GIL全称Global Interpreter Lock为了避免误导,我们还是来看一下官方给出的解释:

In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)

好吧,是不是看上去很糟糕?一个防止多线程并发执行机器码的一个Mutex,乍一看就是个BUG般存在的全局锁嘛!别急,我们下面慢慢的分析。

Read more »

Python 中的进程、线程、协程、同步、异步、回调

上下文切换技术

简述

在进一步之前,让我们先回顾一下各种上下文切换技术。

不过首先说明一点术语。当我们说“上下文”的时候,指的是程序在执行中的一个状态。通常我们会用调用栈来表示这个状态——栈记载了每个调用层级执行到哪里,还有执行时的环境情况等所有有关的信息。

当我们说“上下文切换”的时候,表达的是一种从一个上下文切换到另一个上下文执行的技术。而“调度”指的是决定哪个上下文可以获得接下去的CPU时间的方法

进程

进程是一种古老而典型的上下文系统,每个进程有独立的地址空间,资源句柄,他们互相之间不发生干扰。

每个进程在内核中会有一个数据结构进行描述,我们称其为进程描述符。这些描述符包含了系统管理进程所需的信息,并且放在一个叫做任务队列的队列里面。

很显然,当新建进程时,我们需要分配新的进程描述符,并且分配新的地址空间(和父地址空间的映射保持一致,但是两者同时进入COW状态)。这些过程需要一定的开销。

进程状态

忽略去linux内核复杂的状态转移表,我们实际上可以把进程状态归结为三个最主要的状态:就绪态,运行态,睡眠态。这就是任何一本系统书上都有的三态转换图1

就绪和执行可以互相转换,基本这就是调度的过程。而当执行态程序需要等待某些条件(最典型就是IO)时,就会陷入睡眠态。而条件达成后,一般会自动进入就绪。

Read more »
0%