Archive for February, 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

Multiple recursive many-to-many relationships in djangos ORM

These don’t work – who knew. At least I think this the case

The problem is that the link-backs are constructive from the class name (and can’t be overridden) – so if you has a recursive many link for Majig to itself then an instance of majig has a majig_set attribute for the link-backs. But what happens when you have two such relations.

February 19, 2010 at 11:58 pm Leave a comment

Matplotlib, shared axes and formatters

I Just spent quarter of an hour or so being perplexed an annoyed by matplotlib.

It turns out that if you use the sharex property when creating subplots, plotting on these later plots will override any
previous formatter set on the other plots. (This in a way makes sense – since all the plots axes must be redrawn to take account of the facts that the axis might have grown).

This means that if you want to set a formatter (or a tick locator) this must be done after the final plot that shares an axis.

Bad:

plot1 = subplot(121)
plot(stuff)
gca().xaxis.set_major_formatter(CrazyFormatter)

subplot(122, sharex=plot1)
plot(other_stuff) # destroys the first formatter

Good:

plot1 = subplot(121)
plot(stuff)

subplot(122, sharex=plot1)
plot(other_stuff) # destroys the first formatter
gca().xaxis.set_major_formatter(CrazyFormatter)

Relevant links:

Ticker api
Pyplot api

February 6, 2010 at 7:35 pm Leave a comment


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