Asynchronous file io in eventlet (aka non-thread blockingread)

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()
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。