The I/O Model

看了一下午,很想写篇文总结下,但看得太多了,很多都没有深刻理解,暂且挖个坑吧

Keywords

  • 同步/异步,阻塞/非阻塞
  • Unix 网络编程 5 类 IO 模型:阻塞IO、非阻塞IO、IO多路复用(select/poll等)、信号驱动、异步IO(AIO)
  • epoll, kqueue, IOCP
  • multi processes/threads, coroutine - multi core (parallel), multi machine(distributed, MapReduce)
  • event-driven, NodeJs, Twisted
  • epoll: Tornado web server, Nginx
  • erlang, golang, stackless python, python(gevent eventlet greenlet), python(yield, next, send), lua

Reference

  • http://www.dabeaz.com/coroutines/
  • http://armsword.com/2013/09/14/io-synchronous-and-blocking.html
  • http://www.zhihu.com/question/19732473
  • http://pengjiaheng.iteye.com/blog/847615
  • http://blog.ez2learn.com/2010/07/17/talk-about-coroutine-and-gevent/
  • http://twistedmatrix.com/trac/
  • http://stackoverflow.com/questions/15526546/confusion-about-node-js-internal-asynchronous-i-o-mechanism
  • http://cnodejs.org/topic/4f16442ccae1f4aa2700113b
  • http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
  • http://blog.codingnow.com/2006/04/iocp_kqueue_epoll.html
  • http://www.ibm.com/developerworks/cn/aix/library/1105_huangrg_kqueue/

Manage NodeJs Versions Using NVM

顾名思义啦,NVM(Node Version Manager).

根据 https://github.com/creationix/nvm 可以很方便地安装,

然后可以很方便地安装、管理各版本的node。

1
nvm install v0.10.26

装完后会显示 Now using node v0.10.26,而在新开的 shell 中需要 nvm use v0.10.26。所以设置一个默认版本,nvm alias default v0.10.26 貌似 nvm 只能管理使用 nvm 来安装的 nodes,然后按版本被安装到类似$HOME/.nvm/v0.10.26的 path.

安装 node 会附带安装 npm(Node Package Manager)。npm 安装 package 有两种模式,global 跟 local,local的概念就是只在项目目录下安装一份(其实js的安装也只是简单地拷贝文件嘛),这起到了类似 python venv 的功能,可以为每个项目单独隔离出一个包依赖环境。 那么,为了避免多次安装造成多次download,npm 默认会在 $HOME/.npm 目录下 cache 下载的包。npm cache clean 可以清空这个目录。

使用了 nvm 后, 附带的npm 被安装在了 $HOME/.nvm/v0.10.26/lib/node_modules/npm/ (因为 npm 也只是一个 node module 而 global package 的安装目录变为 $HOME/.nvm/v0.10.26/lib/node_modules,bin 会被 symlink 到 $HOME/.nvm/v0.10.26/bin,它也已经被加入到 PATH 中.

1
npm config get prefix

另外,我原先的 node 都是用 homebrew 装的,

1
2
$ ls /usr/local/Cellar/node/
0.10.17 0.8.14

npm 被装在 /usr/local/lib/node_modules/npm -> /usr/local/Cellar/node/0.10.17/lib/node_modules/npm

npm prefix 为 /usr/local/share/npm,因而 npm global 包被装在 /usr/local/share/npm/lib/node_modules/

OS X Chflags

U盘插过机房的机子后被某Windows上的2b病毒影响了,所有文件都被隐藏然后生成对应的 .lnk 的文件(Windows快捷方式)。 今天插OS X上一看,在图形界面下还是被隐藏的(说明文件/FAT32在OS X上挂载后,文件的隐藏属性跟Windows上的都可以上层兼容?)

OS X 实际上是 FreeBSD,Linux上隐藏文件/文件夹都只有取名为.开头的方法,BSD上还有 chflags 这个命令

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
CHFLAGS(1)                BSD General Commands Manual               CHFLAGS(1)

NAME
     chflags -- change file flags

SYNOPSIS
     chflags [-fhv] [-R [-H | -L | -P]] flags file ...

     ...

     The flags are specified as an octal number or a comma separated list of keywords.  The following keywords are currently defined:

           arch, archived
                   set the archived flag (super-user only)

           opaque  set the opaque flag (owner or super-user only).  [Directory is opaque when viewed through a union mount]

           nodump  set the nodump flag (owner or super-user only)

           sappnd, sappend
                   set the system append-only flag (super-user only)

           schg, schange, simmutable
                   set the system immutable flag (super-user only)

           uappnd, uappend
                   set the user append-only flag (owner or super-user only)

           uchg, uchange, uimmutable
                   set the user immutable flag (owner or super-user only)

           hidden  set the hidden flag [Hide item from GUI]

flag 前面加 no 就是相反的意思, 所以,

1
chflags nohidden *

就行了.

Open Source & Cross-platform UML Tools

呵呵,都到大四了还逃不过么。。依然有门神奇的课需要画一堆UML diagram。。 Well…至少我是不会去用那些 Windows Only 神奇的所谓企业级的软件的。。(真不知道学校都什么心态啊,你种你教学生的时候别用盗版啊

  • http://www.genmymodel.com/

    在线的,略吊,但是目前支持的图有点少,不过大多时候能画类图也就够了

  • http://argouml.tigris.org/

    ArgoUML

  • http://www.umlet.com/changes.htm

    UMLet

  • http://www.astah.net/editions/community

    Astah Community,这个推荐的人还蛮多

  • https://github.com/amolenaar/gaphor

    用我大python写的

  • https://github.com/ObeoNetwork/UML-Modeling

    OpenSource (EPL licensed) UML2 Graphical Modelers (Class, Composite, State, Activity, Sequence …) 这个应该是我找到的功能最全的,界面是继承 Eclipse 的貌似

Trouble With OS X Python Pkgs

起因

安装 https://github.com/amolenaar/gaphor 的时候总报 ImportError: No module named pygtk, 而我的 pygtk 是 homebrew 装的,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ brew info pygtk
pygtk: stable 2.24.0
http://www.pygtk.org/
/usr/local/Cellar/pygtk/2.24.0 (658 files, 18M) *
  Built from source
From: https://github.com/Homebrew/homebrew/commits/master/Library/Formula/pygtk.rb
==> Dependencies
Build: pkg-config ✔
Required: glib ✔, gtk+ ✔, atk ✔, pygobject ✔, py2cairo ✔
==> Options
--glade
    Python bindigs for glade. (to `import gtk.glade`)
--universal
    Build a universal binary
==> Caveats
For non-homebrew python (2.x), you need to amend your PYTHONPATH like so:
  export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

(出问题后看pygtk有几个依赖是叉,还重装过一次)

python -c "import pygtk" 是 ok 的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ echo $PYTHONPATH
/usr/local/lib/python2.7/site-packages:

$ python -c "import sys, pprint; pprint.pprint(sys.path)"
['',
 '/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg',
 '/Users/tclh123/Programming/douban/PyDoubanFM/src/douban-client',
 '/usr/local/lib/python2.7/site-packages',
 '/Users/tclh123/Dev/UML_Tools/gaphor-gaphor-0.17.2',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages',
 '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
 '/usr/local/lib/python2.7/site-packages/gtk-2.0']

sys.path中是包含 ‘/usr/local/lib/python2.7/site-packages’ 的,homebrew 装的 pygtk 在这里面。

但是在 cd gaphor-gaphor-0.17.2 && pip install . 的时候,输出 sys.path

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
['/private/tmp/pip-RS454x-build/setuptools_git-1.0-py2.7.egg',
 '/private/tmp/pip-RS454x-build/nose-1.3.0-py2.7.egg',
 '.',
 '',
 '/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.8-intel.egg',
 '/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg',
 '/Library/Python/2.7/site-packages/distribute-0.6.49-py2.7.egg',
 '/Users/tclh123/Programming/douban/PyDoubanFM/src/douban-client',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages',
 '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']

却不包含 ‘/usr/local/lib/python2.7/site-packages’ 了。。为何

https://github.com/Homebrew/homebrew/issues/10908 这个 issue 是关于 Meld 的,依然没发现什么有帮助的信息么..

python 在 OS X 下的 site-packages 目录

在我机子上分散在下列地方,

1
2
3
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/  # 这个应该是系统自带python默认的地方?
/Library/Python/2.7/site-packages/  # 大部分pip装的似乎都到这里了
/usr/local/lib/python2.7/site-packages/  # homebrew 装的地方

问题还没搞清楚 To be continued

easy_install on OS X

还有,我搞不清楚现在我 easy_install 装的东西是否 work 了,以及装的地方在哪里?

http://lukehagan.com/journal/2010-11-21-easy_install/

似乎依然不 work 迷茫中

Use Haskell on OS X

现在不用一个个装 GHC、Cabal 什么的了,直接装 Haskell Platform.

装完1.2G…略忧伤

下面抄一下介绍..

Haskell Platform for Mac OS X

Welcome to Haskell Platform. The platform consists of the Glasgow Haskell Compiler (GHC) and an extensive set of standard libraries and utilities with full documentation.

Documentation

  • Libraries Documentation for the libraries that come with Haskell Platform & GHC.
  • GHC The GHC User’s Guide has all you need to know about using GHC: command line options, language extensions, GHCi, etc.
  • GHC API Documentation for the GHC API.
  • Cabal An infrastructure for building and distributing Haskell software.
  • Haddock A tool for automatically generating documentation from annotated Haskell source code.

What is Installed

On Mac OS X, the Haskell Platform is installed in two major pieces: GHC and Haskell Platform. They are installed respectively in:

/Library/Frameworks/GHC.framework
/Library/Haskell

Executables are symlinked in /usr/bin and should be available in any shell.

Uninstallation

This and prior versions of GHC and Haskell Platform can be found and then easily removed with the uninstallation command line utility:

/Library/Haskell/bin/uninstall-hs

Simply run it for more information.

Configuring Cabal

The cabal command manages the building and installation of packages, both your own, and those it can fetch from the Hackage repository. The first time you run cabal, a Mac specific configuration is written into the ~/.cabal directory.

  • If this is the first time you have ever run cabal, it will be made your active configuration.
  • If you have run cabal in the past, the new settings are in the file config.platform. You might want to review and incorporate some of the settings into your existing config file, or just replace your config file with it entirely.

The configuration sets up cabal to install packages with the same layout as those installed with the Platform. Packages installed per user (–user, the default) are placed in a parallel tree in ~/Library/Haskell.

N.B. Built executables will be symlink’d into ~/Library/Haskell/bin, you probably want to add that to your $PATH by adding this line to your ~/.bash_profile:

export PATH="$HOME/Library/Haskell/bin:$PATH"

Upgrade Ack on Ubuntu

1
2
3
4
5
6
7
mkdir ~/dpkgs
cd ~/dpkgs

wget http://packages.debian.org/sid/all/libfile-next-perl/download
wget http://packages.debian.org/sid/all/ack-grep/download
sudo dpkg -i libfile-next-perl_1.12-1_all.deb
sudo dpkg -i ack-grep_2.12-1_all.deb

Reference

  • http://stackoverflow.com/questions/7765813/ignoring-sub-directories-in-ackrc#answer-8012284
  • http://askubuntu.com/questions/355668/how-to-install-ack-grep-2-0-with-apt-get
  • http://askubuntu.com/questions/40779/how-to-install-a-deb-file-via-the-command-line

Vim Saving of Files as Sudo

Understanding :w !sudo tee %

Following is copied from Andrew Marshall’s answer on StackOverflow.

% means “the current file”

As eugene y pointed out, % does indeed mean “the current file name”. Another use for this in Vim is in substitution commands. For example, :%s/foo/bar means “in the current file, replace occurrences of foo with bar.” If you highlight some text before typing :s, you’ll see that the highlighted lines take the place of % as your substitution range.

:w isn’t updating your file

One confusing part of this trick is that you might think :w is modifying your file, but it isn’t. If you opened and modified file1.txt, then ran :w file2.txt, it would be a “save as”; file1.txt wouldn’t be modified, but the current buffer contents would be sent to file2.txt.

Instead of file2.txt, you can substitute a shell command to receive the buffer contents. For instance, :w !cat will just display the contents.

If Vim wasn’t run with sudo access, its :w can’t modify a protected file, but if it passes the buffer contents to the shell, a command in the shell can be run with sudo. In this case, we use tee.

Understanding tee

As for tee, picture the tee command as a T-shaped pipe in a normal bash piping situation: it directs output to specified file(s) and also sends it to standard output, which can be captured by the next piped command.

For example, in ps -ax | tee processes.txt | grep ‘foo’, the list of processes will be written to a text file and passed along to grep.

 +-----------+    tee     +------------+
 |           |  --------  |            |
 | ps -ax    |  --------  | grep 'foo' |
 |           |     ||     |            |
 +-----------+     ||     +------------+
                   ||   
           +---------------+
           |               |
           | processes.txt |
           |               |
           +---------------+

(Diagram created with Asciiflow.)

See the tee man page for more info.

Tee as a hack

In the situation your question describes, using tee is a hack because we’re ignoring half of what it does. sudo tee writes to our file and also sends the buffer contents to standard output, but we ignore standard output. We don’t need to pass anything to another piped command in this case; we’re just using tee as an alternate way of writing a file and so that we can call it with sudo.

Making this trick easy

You can add this to your .vimrc to make this trick easy-to-use: just type :w!!.

1
2
3
" Allow saving of files as sudo when I forgot to start vim using sudo.
cmap w!! w !sudo tee > /dev/null %
The > /dev/null part explicitly throws away the standard output, since, as I said, we don't need to pass anything to another piped command.

Tmux Lose Colorscheme

1
alias tmux="TERM=screen-256color-bce tmux"

And set up the default-terminal option in ~/.tmux.conf,

1
2
set -g default-terminal "screen-256color"
# set -g default-terminal "xterm"

Reference

  • http://rhnh.net/2011/08/20/vim-and-tmux-on-osx
  • http://stackoverflow.com/questions/10158508/lose-vim-colorscheme-in-tmux-mode#comment24005530_10264470
  • http://blog.longwin.com.tw/2011/04/tmux-learn-screen-config-2011/

Python String Intern

今天在__builtin__里发现一个intern方法,(原来是internal的意思.. 其实 C 里面有个文字常量区,就是说只要是字符串字面量都会存在那里。 才知道Python里不太一样,比如说

1
2
3
4
>>> a = 'a b'
>>> b = 'a b'
>>> a is b
False

真相是,python只对由0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz中的字符组成的字符串自动做intern。 因为它们可能会更短…更像标识符.. 另外,在 Python 3 中,似乎被移到sys.intern中去了

Reference

  • http://stackoverflow.com/questions/1136826/what-does-python-sys-intern-do-and-when-should-it-be-used/1137293