Tuesday, July 11

Geek

We Hates It

We hates Python scoping rules. We hates them forever.

(Currently going for a doc crawl on the theory that it can't be that broken.)

Huh.

Okay, the problem I'm having involves modules, threads, and thread-specific global data. I haven't solved the problem with modules yet, but it turns out there was some magic added to Python 2.4 for thread-specific globals (threading.local). That's a comfort, because I knew that worked, but I couldn't figure out how. CherryPy, the web framework I'm using, supports this under 2.3, but it turns out that it's a hack and it's very slow. So I'm not going mad. Or at least, no more than usual.

I'm moving Minx from the test design, where it is a single CGI program (and so each request is perfectly isolated and I can slap the code together any which way) to production, as a multi-threaded persistent server. Which is much more fiddly in terms of structuring the code and variables, but is twenty to thirty times faster.

Up to 95% of the time taken by the CGI version is overhead: starting a shell, then starting a Python interpreter, loading the twenty or so libraries used, opening a connection to MySQL, and so on. The multi-threaded version does all of that once. (Or at worst, once per thread, for a persistent thread pool.) It also uses Psyco, the Python compiler, which adds a 30% to 50% speed boost for this sort of app. For the CGI version, Psyco takes long enough to do the compile that the overall performance is worse in most cases...

Only because the threads weren't actually isolated from one another, it didn't work at all. I could either add an extra parameter to all the roughly 100 functions I've written so far, or I could work out how to do thread-specific globals.

Update: Okay, all is forgiven. The threading.local trick works flawlessly, even with modules. Threading-local global data for one module is not visible in another, but even if that complicates things for me, that's right. They're modules, not include files. So I have thread-local module-local global variables... Yay!

Update: And it works perfectly with CherryPy. I expected that, because it only makes sense that CherryPy would be using the standard threading module, but there's a difference between being the only sensible way to do something and actually testing it.

Posted by: Pixy Misa at 08:34 AM | Comments (5) | Add Comment | Trackbacks (Suck)
Post contains 399 words, total size 2 kb.

1 I thought the plain forking CGI fell out of fashion when Backstreet Boys were all the rage.
 http://utcc.utoronto.ca/~cks/space/blog/programming/SCGIvsFastCGI
 (has links)



Posted by: Pete Zaitcev at Tuesday, July 11 2006 01:12 PM (9imyF)

2 Have you ever seen a duck scratch it's head?

Posted by: Wonderduck at Tuesday, July 11 2006 04:23 PM (+rGmJ)

3 Pete - you'd think so, wouldn't you?

But no, there's old-style CGI apps all over the place.  I did it that way just to test the code, but there's a hell of a lot of CGI code in production.

It's like COBOL.  It won't ever go away, because it works.





Posted by: Pixy Misa at Tuesday, July 11 2006 07:22 PM (O0soJ)

4 Mmm... cherry pie.

Posted by: Wonderduck at Thursday, July 13 2006 01:05 AM (+FLIL)

5 "I expected that, because it only makes sense that CherryPy would be using the standard threading module, but there's a difference between being the only sensible way to do something and actually testing it." What? You must work in some weird parallel IT universe where people actually expect software to be written sensibly. Oh, what a glorious paradise Pixyland must be...  

Posted by: TallDave at Friday, July 14 2006 12:53 AM (H8Wgl)

Hide Comments | Add Comment

Comments are disabled. Post is locked.
48kb generated in CPU 0.0159, elapsed 0.4623 seconds.
56 queries taking 0.451 seconds, 331 records returned.
Powered by Minx 1.1.6c-pink.