发布时间:2022-08-09 文章分类:编程知识 投稿人:王小丽 字号: 默认 | | 超大 打印

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()