Posts Mentioning RSS Toggle Comment Threads | Keyboard Shortcuts

  • tin 8:15 am on November 29, 2008 Permalink | Log in to leave a Comment
    Tags: , ,   

    [转载]Ruby的并发的一些基本限制 

    因为是feedburner的feeds,所以我就转过来。

    Concurrency is a Myth in Ruby


    Concurrency introduces parallelism into our applications, and threading is, of course, one way to achieve concurrency. But it turns out that in Ruby, this relation is not transitive: execution parallelism is not the same thing as threading. In fact, if you’re looking for parallelism in your Ruby application, you should be looking at process parallelism instead. So why is that?

    Ruby under the covers: Global Interpreter Lock

    To understand what’s going on, we need to take a closer look at the Ruby runtime. Whenever you launch a Ruby application, an instance of a Ruby interpreter is launched to parse your code, build an AST tree, and then execute the application you’ve requested – thankfully, all of this is transparent to the user. However, as part of this runtime, the interpreter also instantiates an instance of a Global Interpreter Lock (or more affectionately known as GIL), which is the culprit of our lack of concurrency:

    Global Interpreter Lock is a mutual exclusion lock held by a programming language interpreter thread to avoid sharing code that is not thread-safe with other threads. There is always one GIL for one interpreter process.Usage of a Global Interpreter Lock in a language effectively limits concurrency of a single interpreter process with multiple threads — there is no or very little increase in speed when running the process on a multiprocessor machine.

    Deciphering the Global Interpreter Lock

    To make this a little less abstract, let’s first look at Ruby 1.8. First, a single OS thread is allocated for the Ruby interpreter, a GIL lock is instantiated, and Ruby threads (‘Green Threads‘), are spooled up by our program. As you may have guessed, there is no way for this Ruby process to take advantage of multiple cores: there is only one kernel thread available, hence only one Ruby thread can execute at a time.

    Ruby 1.9 looks much more promising! Now we have many native threads attached to our Ruby interpreter, but now the GIL is the bottleneck. The interpreter guards itself against non thread-safe code (your code, and native extensions) by only allowing a single thread to execute at a time. End effect: Ruby MRI process, or any other language which has a Global Interpreter Lock (Python, for example, has a very similar threading model to Ruby 1.9) will never take advantage of multiple cores! If you have a dual core CPU, you’ll have to run two separate processes.

    JRuby is, in fact, the only Ruby implementation that will allow you to natively scale your Ruby code across multiple cores. By compiling Ruby to bytecode and executing it on the JVM, Ruby threads are mapped to OS threads without a GIL in between – that’s at least one reason to look into JRuby.

    Process parallelism

    The implications of the GIL are surprising at first, but it turns out the solution to this problem is not all that complex: instead of thinking in threads, think how you could split the workload between different processes. Not only will you bypass an entire class of problems associated with concurrent programming (it’s hard!), but you are also much more likely to end up with a horizontally scalable architecture for your application. Here are the steps:

    1. Partition the work, or decompose your application
    2. Add a communications / work queue (Starling, Beanstalkd, RabbitMQ)
    3. Fork, or run multiple instances of you application
    4. Not surprisingly, many of the Ruby applications have already adopted this strategy: a typical Rails deployments is powered by a cluster of app servers (Mongrel, Ebb, Thin), and alternative strategies like EventMachine, and Revactor (equivalents of Twisted in Python) are gaining ground as a simple way to defer and parallelize your network IO without introducing threads into your application.

    虽然本文是介绍Ruby1.8、1.9和JRuby对线程的不同实现。但是却清晰的解释了线程安全的意义,还有为什么MRI(或者同样使用GIL的CPython)需要使用多进程模型部署。再延伸我们可以知道Apach上面的Mod_rails(Passenger和Ruby Enterprise Edition)还有Mod_python的神奇之处,他们都hack并实现了使用fork让进程共享内存。最后本文还同样引出了为什么传统Ruby和Python应用只有使用多进程才可以利用多个CPU,还有为什么Twisted和EventMachine使用了单进程单线程+event IO的模型。

     
  • tin 4:07 pm on November 19, 2008 Permalink | Log in to leave a Comment
    Tags: , , , ,   

    最近很迷陈绮贞,买了国内能买到的几张正版专辑。


    可惜的是因为经济状况不好,所以没法去看她的演唱会。之所以写下我的想法,一个是因为总是被她的歌曲所打动,也是要告诉自己我还是有追求的。今天就再回忆一下我对她的歌的回忆。
    最早一次听到她的歌是在05年底一个周五下午从密云回到北京的980长途车上,那是入冬的时候了,车外是棕灰色的北京,车窗上凝结了水汽,我穿的厚厚的坐在车上睡着了,在醒来的时候车到大山子了,耳机传来好听的吉他旋律,然后是一个声音非常奇怪的女声唱歌,歌的旋律也很怪,高潮的部分能听到华丽的冒险几个字。因为她的声音很像童声,所以突然留下了奇怪的印象,很难抹去。而后在我的Meizu E5里面反复的听到这首歌,但是我从来没有去看她是谁。我只记得那一些歌曲是dream4ever的CPOP 9月精选。
    因为懒惰一连几个星期没有更换Mp3里面的歌曲,所以有幸反复听到了华丽的冒险,越发的觉得好听了。突然有一天想看看她到底是谁…看到原来她叫陈绮贞,专辑的名字就是华丽的冒险。这个名字让我有点震撼,因为我女朋友(现在她是我夫人)的名字被包含在她的名字中,所以我觉得很奇怪。
    然后就这样过去了很久,我没有可以的去找她的更多专辑听,也没有想到我会迷她的音乐。
    后来老张同学(我的非常好的哥们)要去英国了,去之前他碰巧弄到了许巍演唱会的门票,地点就在人民大会堂。本来计划我和老婆一起去听,可是碰巧她发烧了,所以我就碰巧和老张一起听了这么一场印象深刻的演唱会。许巍的演唱会绝对是非常非常的牛B,好听的要命。他请到的嘉宾碰巧就是陈绮贞,当时我突然想到原来她的歌我是听过的,而且我很喜欢。忘记她在演唱会唱了什么,因为沉醉在许巍的歌声中。不过我记得和许巍的音乐相反,她的音乐是那么的通透、那么的华丽、那么的忧伤、那么的美妙,所以在回来以后我就种下了一个仔细听听她的音乐的种子。
    回家后我去Verycd下载了她的专辑,好像是花的姿态演唱会实录和华丽的冒险,然后就又过了很久很久,专辑就躺在硬盘上。
    在有一次整理硬盘的时候我又找到了这几个专辑,听了花的姿态,惊叹原来她的歌是这么这么的好听,每一首都好听。然后,从去年就迷到了今年,在知道了她要在北京开演唱会的时候很兴奋,不过我知道我很难去现场看,所以只能先买几张CD来支持一下了,忏悔一下听盗版的岁月。在Google她的背静的时候我才发现她真的过着我所向往的那种有追求的生活,她喜欢Lomo,她也喜欢DIY一个个细节。她有艺术家的气质,可是我没有,我却想追求。在这个时候我才知道她来自台湾,我本一位它来自那个小而清新的新加坡,后来知道她其实是地道的台湾人。我也很喜欢台湾的民谣老将陈升,还有他的徒弟刘若英。陈升在鱼说这张专辑里面翻唱了《花的姿态》,不过冠以《你一直在玩》的名字,初听感觉很怪,就好像第一次在听陈绮贞。跑题一下鱼说这张专辑,让我感动,陈升已经是一个参与到铁人三项里面的歌手了,也曾是个酒鬼,很有趣,他居然这么自由的创作了这样一张专辑。陈绮贞的专辑也是这样的自由,这种自己自由的追求让我感动。
    在开车的时候很喜欢听她的音乐。她的音乐都有标志性的巨好听的前奏,那前奏每次都让我误以为是另外的一首什么歌,让我每次都陶醉在里面,忘记那是什么歌,那是谁在唱,我在干什么。在歌曲进入主题,每次都能听到那种忧伤的情绪,或者说是脆弱或者清脆的理智的小女生的倾诉,我的爱人也是脆弱的女声,所以每次听到这样的音乐都回触动我心里那脆弱的神经,让我想要抱紧我爱的人。别人的博客把她的声音比作软刀子,暴露你的脆弱。好听的旋律倾诉恐怖的情绪,也许我喜欢这样的矛盾,这种偏执的感觉。这种感觉让我想起了Nick cave的谋杀情歌那张专辑,缓慢的民谣下面却是那可以刺激你的神镜的情绪和故事。
    在买了她的几张专辑以后老婆质疑我为什么喜欢一个女歌手,问我她的长相。其实我才想起我一直没有关心过她长什么样子,她的专辑上的照片很少有面部特写,很多都用Lofi的效果处理过去了。其实好听的音乐人让我不敢关心她的长相,不过在我仔细寻找了一些照片以后我发现她的长相并没有让人失望,起码让我有一种想要祝福的感觉,祝福这个美女。跑题。
    无法说出最喜欢哪一首么?其实我最喜欢《旅行的意义》,因为歌词就好像一首叙事的诗。歌词本身也是一首诗,提出这个问题已经足够了,因为我们要活的明白。听说陈绮贞在巴黎被小偷偷窃后追讨钱包还被打过,对她产生了非常大的崇拜之情。我是软弱的,虽然看似坚强,女人看似是软弱的,可是她们之中很多都非常坚强!我也经常问我自己生活所做的各种事情的意义,也包括旅行的意义,在今年旅行时我也一直问我自己旅行到底是为什么?我想我有我的答案。
    写这篇博文是为了让这些记忆的片断告诉我自己我是非常的爱她的歌,非常热爱她的追求,非常的认同她做事的方式,非常向往,向往那份自由的生活。当然所有的这些都是有沉重的责任的,只有很少的音乐人过上了好日子,大部分都还在为温饱而斗争,那么能够在DIY的道路上走到所有人都认同愿意出钱买票去看演唱会的局面是多么的不容易。
    Kudos to cheer!

     
  • tin 5:58 am on November 13, 2008 Permalink | Log in to leave a Comment
    Tags: , ,   

    找到了我的Mac下的git-svn不工作的问题 

    前一段时间把Mingle的svn用git-svn在本地clone了一个git repository,不过后来非常奇怪的是git svn的时候提示命令找不到了。没有在意。

    今天需要用git svn rebase一下这个repository,所以到处搜索为什么?最后发现了问题在于我使用macports安装的git-svn,但是升级的时候却使用了git install,结果造成了系统中安装了两个配置不同的git-core包,而包含git-svn的却没有被激活。

    执行

    tin@tw-dell:git_mingle >port installed The following ports are currently installed: … git-core @1.6.0.2_0+bash_completion+doc+svn git-core @1.6.0.2_0+doc (active) …

    就是说+svn的git-core目前没有激活,那么好办了。

    tin@tw-dell:git_mingle >sudo port deactivate git-core @1.6.0.2_0+doc —> Deactivating git-core 1.6.0.2_0+doc tin@tw-dell:git_mingle >sudo port activate git-core @1.6.0.2_0+bash_completion+doc+svn —> Activating git-core 1.6.0.2_0+bash_completion+doc+svn

    其中先deactivate现在激活的git-core,再activate+svn版本的git就OK了。 如果你还没有安装,这样安装:

    sudo port install git-core+svn+bash_completion+doc

    –EOM–

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel