浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Asynchronous file io in eventlet (aka non-thread blocking read) « Arg and gah and ap and pa

Asynchronous file io in eventlet (aka non-thread blocking read)

February 22, 2010 at 7:26 pm
1 comment

I spent a little while trying to get this to work. I ended up using the following approach that worked quite well

i) Create a thread pool

ii) Use this thread pool to to perform blocking file io

and return this result to your thread

iii) Do something with the result

I do quite like the run-this-on-another-thread-without-blocking-this-thread-and-wait-for-the-result” operation.

Below is some (tested) code for eventlet 0.9.4.

import sys
import eventlet.tpool

def controller():
	while True:
		line = eventlet.tpool.execute(sys.stdin.readline)
		eventlet.spawn(talker, line.strip())

def talker(msg):
	while True:
		eventlet.sleep(1)
		print msg

eventlet.spawn(controller).wait()
posted on 2012-04-30 09:17  lexus  阅读(475)  评论(0编辑  收藏  举报