十一 03

但愿不用等太晚:)

#!/usr/bin/python
 
import urllib2
import sys
import time
 
def alarm():
	while True:
		sys.stdout.write('\a')
		sys.stdout.flush()
 
def poll():
	count=0;
	while True:
		count = count + 1
		now = time.localtime( time.time() )
		print '%02d:%02d:%02d %d' % (now.tm_hour, now.tm_min, now.tm_sec, count)
		try:
			urllib2.urlopen('http://store.apple.com/hk-zh/browse/home/shop_iphone/family/iphone/iphone4s')
			alarm()
		except urllib2.HTTPError as error:
			pass
			#print error
 
		try:
			iphone4 = urllib2.urlopen( 'http://store.apple.com/hk-zh/browse/home/shop_iphone/family/iphone/iphone4' )
			html = ''.join( iphone4.readlines() )
			if html.find( 'iPhone 4S' ) != -1 :
				alarm()
		except:
			pass
 
		#time.sleep(1)
 
if __name__ == '__main__':
	poll()
Tagged with:
15

Randy Pausch是CMU的professor,2007年的时候46岁。
和Jobs一样,不幸患上了胰腺癌。
比Jobs更不幸的是,他所患的这种疾病不是罕见的那种,并不能通过手术治愈。

在告别自己无限留恋的妻儿之前,他准备并在CMU做了“The Last Lecture”的演讲,主题是如何实现自己儿时的梦想。
演讲时,他面对的是自己的妻子、朋友、同事和学生;实际上,他是在和未来的孩子对话。
当时,他的孩子分别是5、3、1岁。

他知道,这个演讲会被摄像机记录下来。很多孩子们当时无法明白的话,终有一天可以被理解。
跨越时空的言传身教,伟大的父亲。

从Professor Randy幽默的演讲里,可以领悟到太多的智慧。
具体是什么道理,就请自行领悟吧。

如果看过视频之后,想对Professor Randy了解更多,可以访问http://en.wikipedia.org/wiki/Randy_Pausch

与演讲相应,有一本已经出版的书,也值得阅读和收藏http://book.douban.com/subject/3034706/

Tagged with:
04

算法的思想来自与和同事的一次聊天,做了改进,使得精度可以通过参数控制。

Frequence.h

#pragma once
 
class Frequence 
{
  public:
	Frequence( int precision = 10 );
	~Frequence( );
 
	int Check( );
 
  private:
	int		_precision;
	int 	_slot_num;
 
	struct stSlot
	{
		int count;
		int time;
	};
 
	stSlot *_slot;
 
	inline int calculate( int now );
 
	inline int get_current_slot_num( int now );
};

Frequence.cpp

#include "Frequence.h"
 
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
 
static const int kMinute = 60;
 
Frequence :: Frequence( int precision )
	:	_precision( precision )
{
	if( _precision > 60 ) _precision = 10;
	if( _precision <= 0 ) _precision = 10;
 
	_slot_num = kMinute / _precision + 1;
	_slot = ( stSlot * )calloc( _slot_num, sizeof( stSlot ) );
}
 
Frequence :: ~Frequence( )
{
	free( _slot );
}
 
int Frequence :: Check( )
{
	int now = time( NULL );
	int current_slot_num = get_current_slot_num( now );
	stSlot *current_slot = _slot + current_slot_num;
 
	int diff = now - current_slot->time;
	if( diff > kMinute )
	{
		current_slot->count = 1;
	}
	else
	{
		current_slot->count++;
	}
 
	current_slot->time = now;
 
	return calculate( now );
}
 
int Frequence :: calculate( int now )
{
	int current_slot_num = get_current_slot_num( now );
	stSlot *last_slot = NULL;
 
	int total = 0;	
	int index = 0;
	while( index < _slot_num - 1 )
	{
		int last_slot_num = ( _slot_num + current_slot_num - index ) % _slot_num;
		last_slot = _slot + last_slot_num;
		int diff = now - last_slot->time;
		if( diff > kMinute )
		{
			last_slot->count = 0;
		}
 
		total += last_slot->count;
		index++;
	}
	return total;
}
 
int Frequence :: get_current_slot_num( int now )
{
	return ( now % ( _slot_num * _precision ) ) / _precision;
}
24

好吧,大家(看到这些文字的地球人)都知道Nginx是什么用的了。
这里只是希望可以稍微详细的记录下收集到的信息。
毕竟在一头扎进源码的深潭之前,先在水面观察一阵并不是什么坏事:)

Nginx不是一个普通HTTP Server,它还是一个免费、开源和高效的HTTP Server。
免费和开源是显然的事实,因为源码可以从官网上下载;
高效不能一眼看出,但也有测试数据和市场份额做间接的支持。

Nginx不仅仅是一个HTTP Server,它还可以用作Reverse Proxy和Mail Proxy。
作为Reverse Proxy,主要用于负载均衡、SSL支持、Cache等方面;
Mail Proxy了解不多,暂不妄言:)

根据官网的数据,目前Nginx的市场份额为7.5%。这个数据也和netcraft.com上的调查结果一致。
详细数据可以参考http://news.netcraft.com/archives/2011/01/12/january-2011-web-server-survey-4.html

顺便截下最新的分布。Nginx排名第三,而且势头良好。
不过如果想对领先的Apache和IIS构成威胁,则还是非常遥远的梦想。
nginx market share

项目从2002年开始,2004年开源。
初始作者和主要维护人名叫Igor Sysoev,俄罗斯人,70年生,目前在莫斯科。
可以在http://sysoev.ru/en/找到更多的资料。
不过如果想对个人有更更多的了解,最好懂点俄文。
这对东北的同学来说可能不是难事:)

对这位牛人的相貌比较好奇,Google到的结果让人无从选择。
好在这个星球上,除了Google,还有一个叫Facebook的网站:)
igor
据他在Facebook上的资料显示,他和Intel有某种联系,莫非跳槽了?

发音方面,和Linux一样,最好还是采用发明人的说法,“Engine X”。
挺酷的样子。

作为一个服务器,Nginx的优点非常突出。
http://wiki.nginx.org的说法,高性能、稳定、功能丰富、配置简单,并且节省资源。
难道世界上真有只好不坏的东西?

它使用事件驱动的异步模型,而不是多进程/线程竞争接入请求的架构,解决了C10K的问题。
从官网的描述可以看出,Igor对此架构非常自豪。
不过从现在看来,这样的架构被没什么超凡入圣之处,公司内可以看到很多类似的实现。
甚至可以说,这已经是常规的做法了,
可能回到2002年的时候会是一个非常创新的设计。
事件驱动模型在《Unix Network Programming》里也有详细的阐述,
只是没有提到epoll、kqueue等新的方法,原理都是相通的。

现在,Nginx的最新稳定版本是0.8.54,可以在http://nginx.org/en/download.html下载。
http://nginx.org/的更新历史看,作者非常活跃,每个月起码更新一次。
开发版本已经到了0.9.5。

最后,对于想分析Nginx源码的中国人,http://code.google.com/p/nginxsrp/wiki/NginxCodeReview似乎是个不错的开始。
英文方面,可以看http://nginx.org/en/docs/introduction.html,是Igor写的三篇文章。
美中不足的是,这三篇文章主要都是介绍配置方面信息的,对架构、代码甚少涉及。

Tagged with:
22

分析Nginx源码这个念头曾经出现过,也用GDB配合Source Insight跟踪过两天时间。
之后由于其他事情占据了时间,就搁置了下来,一晃就几个月。

做事过于随性是老毛病了,不过就像许多其他事情,虽明知不妥,却总也戒不掉,改不了。
借着写Nginx模块的惯性和余温,但愿这次可以一口气完成吧。
//Mac OS自带的输入法真不好用,公司们(Google, Tencent, Sogou, Baidu …)大发慈悲,做一个输入法吧

Nginx的代码写得好是出了名的:命名规范、模块化划分、函数简短、适当注释⋯⋯
虽然有如此多的好处,但要对其代码做详细分析,既可俯瞰架构、又能深入细节,却实属艰难。
单单是内存池、回调机制、多重指针等之前几天让我费劲脑汁的技巧,已经让我超出预算。
况且了解细节易,纵观全局难,注定了是一个上下求索的过程。
//这个说法也适用于程序员生涯。

为免自己半途而废,写下以上文字,算是立字为据,也权当之后笔记的前言吧。

Tagged with:
13

by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

just “import this”:)

Tagged with:
22

错过了买票,只能上土豆找,居然也有发现
都是佛山的同学拍的,效果还不错

开场30分钟

第二Part

update:

Youku上也有发现

Tagged with:
14

有这样一个镇子,做什么事情都被禁止了。
现在,因为惟一未被禁止的就是尖脚猫游戏,所以镇上的臣民就经常聚在镇后边的草坪上,成天地玩尖脚猫游戏。
因为禁令被制订的时候总有恰当的原因,所以没有任何人觉得有理由抱怨,也没人觉得受不了。

几年过去了。有一天,官员们觉得再没有任何理由禁止臣民做这些事了,他们就派了传令官四处通知人们一切都开禁了。
传令官来到老百姓喜欢聚集的那些地方。
“听好了,听好了,”他们宣布,“所有的都开禁了。”但人们还是玩尖脚 猫游戏。
“明白吗?”传令官重申,“你们现在可以任意做想做的事了。”
“好的,”臣民们回答。“我们玩尖脚猫。”

那些传令官一再地提醒他们的臣民,他们又可以回到他们从前曾经从事的那些高尚而有用的职业中去了。但是老百姓都不愿听,他们继续玩尖脚猫,一圈又一圈,甚至都不停下来喘口气。
看到他们是白费劲了,那些传令官就回去禀报上面。
“这很容易,”那些官员们说,“现在我们下令禁止尖脚猫。”

人民就是在那时开始反抗的,杀了部分官员。
然后人民分秒必争地又回去玩尖脚猫游戏了。

有种看《Monster》时的诡异感觉,让我误以为卡尔维诺是东欧人。
另外,尖脚猫这个游戏是怎么玩的?

14

升级到Snow Leopard的时候,找到的是dmg文件。
Mac自带的磁盘工具支持将dmg写入到USB stick,并用USB stick来引导安装。

当时就想,是不是iso文件也可以这样做?
今天准备装Ubuntu 10.10的时候,在Ubuntu的安装向导中找到了答案:

  1. Download the desired file
  2. Open the Terminal (in /Applications/Utilities/ or query Terminal in Spotlight)
  3. Convert the .iso file to .img using the convert option of hdiutil (e.g.,hdiutil convert -format UDRW -o ~/path/to/target.img ~/path/to/ubuntu.iso)
  4. Note: OS X tends to put the .dmg ending on the output file automatically.
  5. Run diskutil list to get the current list of devices
  6. Insert your flash media
  7. Run diskutil list again and determine the device node assigned to your flash media (e.g. /dev/disk2)
  8. Run diskutil unmountDisk /dev/diskN (replace N with the disk number from the last command; in the previous example, N would be 2)
  9. Execute sudo dd if=/path/to/downloaded.img of=/dev/rdiskN bs=1m (replace /path/to/downloaded.img with the path where the image file is located; for example, ./ubuntu.img or ./ubuntu.dmg).
  10. Using /dev/rdisk instead of /dev/disk may be faster.
    If you see the error dd: Invalid number '1m', you are using GNU dd. Use the same command but replace bs=1m with bs=1M.
    If you see the error dd: /dev/diskN: Resource busy, make sure the disk is not in use. Start the ‘Disk Utility.app’ and unmount (don’t eject) the drive.
  11. Run diskutil eject/dev/diskN and remove your flash media when the command completes
  12. Restart your Mac and press alt while the Mac is restarting to choose the USB-Stick

从第5步起,也可以直接用“磁盘工具”代替
原文地址:http://www.ubuntu.com/desktop/get-ubuntu/download

另外,上文也提到了在Windows下将iso做成可引导USB stick的方法。用这个小工具应该比UltraISO要方便吧。

Tagged with:
09

趁着国庆长假,看完了这本书。

作者以工程师、科学家的身份,从非技术的角度,描述了现代IT业的兴衰成败。
尤其是其中关于硅谷背景、风投运作和华尔街对IT公司影响的章节,让作为工程师的我了解到很多平时接触不到的内幕。
虽然有不少错别字(又一个懒得学五笔的人?),依然强烈推荐。
希望作者抽空完成剩下的部分,只是不能再发到Google黑板报了。

下载:浪潮之巅1-18

PS. 作者吴军最近加盟了我们公司:)

Tagged with:
preload preload preload

无觅相关文章插件,快速提升流量