协程(1)

coroutine 协程是什么? 官方是如何实现协程 native coroutine generator-based coroutine 其他协程库 greelet 目前还有哪些坑? 背景 处理io密集问题一般有三种模式: 多进程 多线程 io复用+单线程回调 有了协程后,可以使用io复用+单线程搭配协程。 为什么不用回调呢? 步骤多或者嵌套深,捕捉异常或者阅读代码都会很困扰。而使用协程,让原来要使用异步+回调方式写的复杂代码,可以用看似同步的方式写出来。堆栈信息也很清晰,便于编写和维护 协程是什么 下文是根据官方的实现所定义,与greenlet, golang的goroutine不同 协程直接给它一个定义似乎不太容易,我会从它的运行方式及外沿来描述它。 在单线程中,过程A可被过程B打断,并保存此时过程A的上下文,且执行过程B,稍后过程B交回控制权,恢复过程A的上下文并从过程A的中断处继续执行。这是协程的运行方式。 换句话说,协程就像一个可被打断后继续执行的函数。一个线程中可以开多个协程,但某一时刻只有一个协程在运行。 如何实现协程 目前Python官方的协程实现方式有两种: generator-based coroutine native coroutine generator-based coroutine yield 要明白基于生成器的协程的工作方式,得先弄清生成器是怎么工作的,要说清楚生成器是怎么工作还得说明Python代码基于cpython是怎么执行的。 python代码会被编译成bytecode。 C程序中执行Python函数的函数叫PyEval_EvalFrameEx,当其遇到bytecode为CALL_FUNCTION,会在Python堆栈上新建一个栈帧 1 2 3 4 5 6 7 8 9 10 11 12 13 14 >>> def bar(): ....

December 20, 2018 · 2 min · 341 words · Me

机器学习基石第二章

Perceptron 1 之前已经了解到,机器学习的过程是: 输入一笔数据,这笔数据由\(\mathcal X\)和\(\mathcal Y\)组成,而x和y服从一个未知的分布\(\mathcal f\) 通过learning algorithm \(\mathcal A\)在hypothesis set \(\mathcal H\)中挑选一个合适的g,这个g即是机器学习到的,希望这个g能够很接近f,以至于能替代它。 机器学习的模型是由Learning algorithm \(\mathcal A\)和 hypothesis \(\mathcal H\)组成 今天就要学习一个模型,既然是模型,学完后就要清楚地知道它的hypothesis set 和 learning algorithm 1.Perceptron Hypothesis set 1.1抽象解释 情景:如上图所示,已经了解客户的年龄,工资,工作年限,负债情况等,达到银行的标准,则通过他的信用卡申请。那么如果让机器来做,它要做的是什么呢? > 回顾机器学习的过程,我们需要有输入数据D(x,y),Hypothesis \(\mathcal H\), learning algorithm \(\mathcal A\)。那么一一与这个情景对应,应是怎么样呢? 机器: 把每个申请者当做一个向量\(\mathcal X\),把申请者的信息当做不同维度的特征,则\(\mathcal X = (x_1, x_2, x_3,\dots, x_n)\) 把同意申请设为\(+1\),不同意申请设为\(-1\), 则\(\mathcal Y: \{+1(good), -1(bad)\}, 0 忽略\) 银行的标准即为阈值(threshold) 不同维度的特征应该有不一样的权重\(\mathcal w\) 那么 \[ \mathcal H: h(x) = sign((\sum_{i=1} ^n w_i x_i) - threshold) \]...

February 22, 2017 · 2 min · 401 words · Me

vim command list

vim 分屏 vsplit split 分屏调整 ctrl-w + = vertical resize + or - col resize + or - line 移动 h,l,j,k b or ge - 向前一个单词 w or e - 向后一个单词 { } - beginning of previous,next paragraph $ - 行尾 ^ - 行头第一个非空白字符上 0 - 本行第一个字符上 插入 i a - insert before, after cursor I A - 行首, 行尾 O - insert above current line o - insert in newline R - replace characters starting at the cursor

March 25, 2016 · 1 min · 79 words · Me

Compile the Linux Kernel

Compile the Linux Kernel download the linux kernel source code cd the source code dir make distclean make menuconfig or make oldconfig make all -j4 or make then make modules_install cp -v arch/x86_64/boot/bzInage /boot/vmlinuz-linux-version cp system.map /boot/System.map-version cp .config /boot/config-version update-grub reboot bindeb-pkg or binrpm-pkg could substitute step 5 to step 9. Then dpkg -i kernel.deb or rpm -i kernel.rpm

March 23, 2016 · 1 min · 60 words · Me

hexo博客搭建

hexo博客搭建 hexo原始目录 vps存放public目录 本机向github提交原始文件 本机向vps提交public目录 在vps利用git hooks 自动部署 nginx直接映射public

March 12, 2016 · 1 min · 9 words · Me