Archive for February 22, 2010

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

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

Advertisements

February 22, 2010 at 7:26 pm 1 comment


February 2010
M T W T F S S
« Jan   Mar »
1234567
891011121314
15161718192021
22232425262728