stackless多核测试
时间:2009-06-19 来源:nahuat
  #!/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
"""
  
  # -*- 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
"""
 相关阅读 更多 + 
    
  









