Thursday, 30 September 2010

Junk food update

Just the junk food photos that have accumulated on my camera recently.

I went into City Shop with the idea of buying some Cadbury chocolate. They didn't have any, but they did have a range of interesting Milka flavours. This was probably for the best, as Cadbury has unfortunately come to favour lower production costs over actually making a product worth eating. Ever since the debacle where they moved production of their Chocolate to Australia, and decreased the actual.. chocolate in their chocolate replacing it with palm oil or some similar thing, I cannot muster the interest to buy their "chocolate".

Anyway, yogurt chocolate. It just sounds right. I think this was ~21 RMB.

2010-09-17 - Shanghai - Junk Food - 01 - Milka Jogurt chocolate


And it looks pretty promising. Unfortunately, the flavour is somewhat nondescript. I expect a yogurt product to be tangy and more yogurty. But I guess when you approximate it as a chocolate bar filling, you can only get so close. Still, it was worth trying and I wouldn't say I wouldn't buy it again.

2010-09-17 - Shanghai - Junk Food - 02 - Milka Jogurt chocolate


One thing I used to buy, everytime I flew back home for Xmas was Daim candies. You couldn't buy them there. So Daim flavoured chocolate seems like a natural thing to do. I think this was also ~21 RMB.

2010-09-17 - Shanghai - Junk Food - 03 - Milka Daim chocolate


But really, if you can actually buy Daim candies, I don't know why you would buy this. Daim candies are simply more "Daimy" than this hybrid product. But still, lacking ability to source the candies, this is a reasonably priced simulacrum.

2010-09-17 - Shanghai - Junk Food - 04 - Milka Daim chocolate


I felt like something chocolatey. So I chose this, as it was relatively reasonably priced at ~12 RMB if I recall correctly.

2010-09-24 - Shanghai - Junk Food - 01 - Oreo wafers


The flavour and sensation of eating these, was simply that of eating some cocoa flavoured factory product. There was no pleasure in the act, and doing so made me think of some factory floor where they pressed these out of some slop.

2010-09-24 - Shanghai - Junk Food - 02 - Oreo wafers


I am heading back to City Shop tomorrow, so I'll try and limit myself to one block of Milka. Assuming there are flavours I have not tried yet.

Reading from non-blocking sockets

Until recently, I had never actually used my Stackless socket module for anything other than my own hobbyist projects. These are done out purely of interest, and the limited networking traffic they receive is solely me testing them. I had also never received any complaints about the throughput of the module. So it was a complete surprise to me when I learnt that I was doing non-blocking IO incorrectly with Python's socket module.

I had always assumed that when a call to select indicated there was data to be read, this meant that it was okay to do a recv call. And for my purposes, this appeared to work fine. But when you are polling select thirty times a second, and the packet sizes that are received are consistently around 16 KB, that limits the throughput to 280 KB/s. For instance, an application on an embedded device might be fetching 115 MB in updates. This would take 4 minutes.

It turns out that when select indicates there is data waiting to be read on a socket, you can call recv until EWOULDBLOCK is raised. Who knew? Probably someone who had read appropriate documentation, this document seems to be pretty pertinent.

With the above implemented, the current implementation reads the 115 MB in 13 seconds. And that is with it limiting the number of recv calls within each poll of asyncore, in order to yield in a timely fashion to the application that is pumping it.

Thursday, 23 September 2010

Reconsidering thread blocking WRT microthreads

I meant to spend a good part of the day doing the long overdue Stackless 2.6.6 merge, but having forgotten my SSH key, I found myself at work on a public holiday with time set aside for it.. but with an extra obstacle in the way. So I moved onto another project -- looking into the reports that there are problems with my stackless socket module, and the xmlrpc standard library. I was however unable to reproduce the problems, and spent the time cleaning up the mechanisms that allow me to run the standard library unit tests against my module.

The bulk of these mechanisms are monkey-patchings of thread blocking operations. When any of these calls (time.sleep, select.select, ..) are made, they would block the Stackless scheduler running on that thread. So the general brute force solution is to make any of these calls simply block the current microthread, while the actual call is made in another thread. This is rather naive, but with a bit of special casing for hiding harmless errors caused by race conditions, for the purposes of running suites of unit tests it works out fine.

However, the more I work on something like this, the more uneasy I feel with the long held ideal of having a suite of these monkey-patching mechanisms which users can just install to make any use of the standard or third party libraries just work. It's not that it wouldn't just work for the most part, look at stackless socket, it has been a cobbled together mess for many years and it was more than enough for almost anyone who used it. It's that it is actually important for people writing code to know where the points their microthreads block are. Whether these are safe voluntary yields to the scheduler, where state may change before the microthread gets scheduled again and resumes. Or whether it is unsafe blocking of the thread, which may also block the mechanism which triggers the condition that would reawaken it.

With this in mind, now I am thinking of an alternate solution. Rather than making a library that monkey-patches all thread blocking calls into tasklet blocking calls, I am now leaning towards monkey-patching them to raise an exception. They would all be guarded against being called at all. Any use of them would have to be explicitly allowed. So you would for instance allow stackless socket to call select.select via asyncore.poll, but when you subsequently invoked xmlrpc_server.serve_forever it would error on its call to select.select.

At this stage, no farming off work is performed at all, guards would simply be added to applicable functions. And references to those functions would be tracked down, in order to ensure that there are no corner cases where potentially thread blocking callables escape into the wild. What I mean by this is where for instance threading imports time and stores a local reference to sleep before the guards are put in place. This is a case I had to fix today to get the xmlrpc unit tests to run against stackless socket.

Once this is implemented and tested, next steps might be a generic mechanism to farm guarded calls out to worker threads, and an embedded web server that summarises what blocking calls are being made and additional information that helps a programmer understand what is really going on in their Stackless application.

Right, time to start coding..

Edit: First draft is checked in. It has a minimal blacklist, and can currently only handle simple functions. Before moving onto the next steps listed above, I should make it handle more than that.