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

Advertisements

Entry filed under: Uncategorized. Tags: , , , , , .

Multiple recursive many-to-many relationships in djangos ORM Human readable datetimes in python’s django

1 Comment Add your own

  • 1. Which Linden  |  February 22, 2010 at 11:32 pm

    Cool, yeah! We were just discussing at our sprint whether or not it would be good to implement eventlet.green.os.open() this way.

    If you wrapped it with a tpool.Proxy object, it might be even more convenient:

    nonblocking_stdin = tpool.Proxy(sys.stdin)
    nonblocking_stdin.readline()

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


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

%d bloggers like this: