node.js为什么不用coroutine?
node.js和coroutine
风格之争:Coroutine vs Callback
Call/CC与Node.js
最早听说协程的概念来源于Lua,后来逛论坛听说了stackless python,再后来听说erlang也是用这种方法,但是一直没怎么细看。正好要准备个年后的python的分享,中间涉及到pypy和stackless python,又得扯到协作式多线程(微线程,协程)的概念,便又了解了一番。
node.js为什么不用coroutine?
node.js和coroutine
风格之争:Coroutine vs Callback
Call/CC与Node.js
最早听说协程的概念来源于Lua,后来逛论坛听说了stackless python,再后来听说erlang也是用这种方法,但是一直没怎么细看。正好要准备个年后的python的分享,中间涉及到pypy和stackless python,又得扯到协作式多线程(微线程,协程)的概念,便又了解了一番。
https://github.com/rspivak/slimit
灰常好的工具,压缩比高,代码兼容性好,可媲美主流压缩工具~
做了下简单的benchmark:
#raw 256K jquery-1.8.1.js #download from jquery.com 92K jquery-1.8.1.min.js 36K jquery-1.8.1.min.js.gz #http://closure-compiler.appspot.com 84K jquery-1.8.1.google.advanced.js 32K jquery-1.8.1.google.advanced.js.gz 92K jquery-1.8.1.google.simple.js 36K jquery-1.8.1.google.simple.js.gz #http://dean.edwards.name/packer 120K jquery-1.8.1.packer.js 40K jquery-1.8.1.packer.js.gz #http://refresh-sf.com/yui #Uncaught SyntaxError: Unexpected token } 64K jquery-1.8.1.yui.js 24K jquery-1.8.1.yui.js.gz #slimit 96K jquery-1.8.1.slimit.js 36K jquery-1.8.1.slimit.js.gz
yui的虽然最nb,拉开其他工具一大截,但是压出来已经不能用了,难道是我点击提交按钮的方式不对??
实际使用时,我用的环境是redhat5.x,python环境较为恶劣,slimit只兼容到python 2.7,于是单独做了一个”便携版本”,可以兼容到2.4,并且相关依赖也放到一起,下载即用:)
https://github.com/emptyhua/js-css-minify-portable
最近做了一个webim的项目,使用长连接做push,在chrome和ie下有些小问题
1. chrome的左下角下一直显示等待push.xx.com的响应
解决方法:接到请求立刻返回http头就可以了,不等到有了推送再返回http头
2.ie下对于每个域名同一时刻最多只能有两个并发请求,如果开多个窗口(这个限制不是针对单个窗口的),有的请求便会被阻塞。
解决方法:对每个窗口使用不同的请求域名
使用三级泛域名配置起来最简单,例如:123.push.xx.com.但是JS里涉及到跨域的问题,所以还是要使用push-123.xx.com这样的二级域名。查了下bind9的手册可以这么搞
;示例
$GENERATE 0-100000 push-$ IN A xx.xx.xx.xx
这样ie下JS可以用时间戳或随机数决定一个domain去请求,不会产生阻塞
发现有时候做非主流还真痛苦。。
现在用的这个vps是centos5 + 免费的kloxo主机管理系统。kloxo用的lighttpd,所以wp-super-cache的那套apache的rewrite规则就完蛋了~
于是搜lighttpd的重写规则,找到了一个老外的帖子, 需要启用mod_magnet(这个模块可以使用lua脚本进行更强大的url重写)
于是我把/etc/lighttpd/lighttpd.conf里mode_magnet的注释删掉
server.modules = ( "mod_rewrite", "mod_redirect", "mod_alias", "mod_access", "mod_magnet", ... ...
然后
/etc/init.d/lighttpd restart
我得到了下面的错误
mod_magnet plugin init failed
于是我继续google,发现需要重新编译下lighttpd,郁闷了。。
安装lua,发现有现成的包
wget http://packages.sw.be/lua/lua-devel-5.1.4-1.el5.rf.i386.rpm
rpm -i lua-devel-5.1.4-1.el5.rf.i386.rpm
wget http://packages.sw.be/lua/lua-5.1.4-1.el5.rf.i386.rpm
rpm -i lua-5.1.4-1.el5.rf.i386.rpm
yum安装编译工具和其他依赖
yum install gcc glib2-devel autoconf automake libtool openssl-devel pcre-devel bzip2-devel gzip-devel zlib-devel
编译安装lighttpd
wget http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.25.tar.gz
tar -zxvf lighttpd-1.4.25.tar.gz
cd lighttpd-1.4.25
./configure --with-pcre --with-lua --with-zlib --with-openssl --sbindir=/usr/sbin --sysconfdir=/etc/lighttpd
make
make install
运行完毕,就可以把原有的lighttpd覆盖掉了,再次启用mod_magnet,成功~
然后就是配置这个重写的lua脚本,老外给的那个脚本是有问题的,所以自己又修改了下,大体的配置如下
$HTTP["host"] =~ "^(www.bluehua.org|bluehua.org)" {
#重写开始
server.error-handler-404 = "/index.php"
magnet.attract-physical-path-to = ("/document-root/rewrite.lua")
#结束,就这就两句管用了
.......
}
最后我改过的脚本下载:rewrite.lua
总算是好使了。。
最近好像总是发些shell的东西,木办法,最近没怎么写js的样子…
————————————————————–
恩,之所以要替换成绝对路径,是因为css和图片host在不同域的server上,写完刚在站里的静态跑过,好像木啥大问题~
效果:
/img/a.gif 会被替换成 http://domain/img/a.gif ../img/a.gif 会被替换成类似 http://domain/parent/img/a.gif
使用方法如下(linux only)
#直接跟目录就好了
sh r2a.sh /opt/static
脚本下载点这里:r2a.zip
贴出shell的源码,便于不想下载又想看看shell的同学,还有个python脚本,就不贴了,没啥含量
#!/bin/sh
#@author emptyhua@gmail.com
#本地根目录
LROOT="/home/hualu/work/static"
#远程根目录
RROOT="http://myimgdomain.cn"
#要处理目录的绝对路径
CP="`cd $1;pwd`"
#用于转换相对路径为绝对路径的python脚本的路径
PY="/home/hualu/bin/get_sed_rule.py"
LROOTR="`echo ${LROOT} | sed 's/\\//\\\\\//g;s/\\./\\\\\\./g'`"
RROOTR="`echo ${RROOT} | sed 's/\\//\\\\\//g;s/\\./\\\\\\./g'`"
echo "执行路径${CP}"
find ${CP} -name "*.css" > /tmp/_r2a_css_files
cat /tmp/_r2a_css_files | while read f;do
echo "处理:${f}"
dir="`dirname \"${f}\"`"
#获取css文件所在的发布目录
echo "${dir}" | sed "s/${LROOTR}/${RROOTR}/" > /tmp/_r2a_remote_path
remote_path="`cat /tmp/_r2a_remote_path`"
#收集要处理的图片路径
cat $f | awk '{print NR,"####",$0}' | grep "url([\"\']*\.\{1,2\}" > /tmp/_r2a_file
cat /tmp/_r2a_file | sed -e "s/url([\"\']*\(\.\.[^\)]*\)[\"\']*)/开始\1始开/g" -e "s/####[^始]*开始/:/;s/始开[^始]*$/:/;s/始开[^始]*开始/:/g" > /tmp/_r2a_cssimage
echo '' > /tmp/_r2a_sed_rule
#将相对路径转换为绝对路径,并生成sed语句
cat /tmp/_r2a_cssimage | while read r;do
rule="`python \"${PY}\" \"${r}\" \"${remote_path}\"`"
echo "${rule};" >> /tmp/_r2a_sed_rule
done
sed_rule="`cat /tmp/_r2a_sed_rule | tr -d '\\n'`"
#利用sed处理文件
sed -i -e "$sed_rule" "${f}"
sed -i -e "s/url(\([\"\']*\)\//url(\1${RROOTR}\//g" "${f}"
done
rm /tmp/_r2a_css_files
rm /tmp/_r2a_remote_path
rm /tmp/_r2a_file
rm /tmp/_r2a_cssimage
rm /tmp/_r2a_sed_rule