Chinaunix首页 | 论坛 | 博客
  • 博客访问: 212902
  • 博文数量: 70
  • 博客积分: 2050
  • 博客等级: 大尉
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-15 21:42
文章分类

全部博文(70)

文章存档

2013年(1)

2011年(5)

2010年(3)

2009年(9)

2008年(17)

2007年(6)

2006年(29)

我的朋友

分类: Python/Ruby

2009-06-19 18:34:26

#!/usr/bin/pypy-stackless
# -*- coding: utf8 -*-

import stackless, time

c1, c2 = stackless.channel(), stackless.channel()

def log(msg):
    t = time.time()
    print '[%s.%06d]' % (time.strftime('%T', time.localtime(t)), (t - int(t)) * 1000000), msg

def consumer(name, channel):
    log('entry ' + name)
    log(channel.receive())
    time.sleep(3)
    log('leave ' + name)

def productor():
    log('entry productor')
    c1.send(('hello', 'world'))
    c2.send(('foo bar',))
    log('leave productor')

stackless.tasklet(productor)()
stackless.tasklet(consumer)('one', c1)
stackless.tasklet(consumer)('two', c2)
stackless.run()

"""
以下是执行结果
[18:28:38.464008] entry productor
[18:28:38.464564] entry one
[18:28:38.464715] ('hello', 'world')
[18:28:41.466892] leave one
[18:28:41.467211] entry two
[18:28:41.467592] ('foo bar',)
[18:28:44.470731] leave two
[18:28:44.471030] leave productor
"""
阅读(674) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~