Hopkins Weblogs


Recent Posts

  • Threads and SQLite
    from phf by phf
    One great thing about teaching is that as you tell students how to do something, you constantly get to re-evaluate what you did yourself in a similar situation. Case in point: I was talking to my students in Unix Systems Programming about multi-threaded producer-consumer systems and how to use queues to coordinate them. While going through some examples, I noticed that I had made a really bad call some time ago when I integrated an SQLite database with a multi-threaded web application. Some background? I have a web application written in CherryPy, a very nice but also very multi-threaded Python framework. I decided to use SQLite as the database for my application because I didn't want to deal with the complexities of setting up MySQL or something similar. You may say "That's your mistake right there!" but hey, it's what I did and I don't want to change databases right now. (I also don't want to switch the application to some ORM at this point, but of course I should probably have used SQLAlchemy from the beginning.) In case you don't know: SQLite doesn't like multiple threads to begin with as it uses a global lock for the whole database. Also, the Python interface to SQLite doesn't like multiple threads: You can't share objects created through the interface among multiple threads. So I had to do two things: Get each CherryPy thread its own database connection (the only way to generate more SQLite objects). Handle the (inevitable) case that two threads want to access the database concurrently. The first was easy to solve: I maintain a dictionary of database connections indexed by thread. When a thread wants to execute a query, I open a connection for it if it doesn't have one already. The only "problem" here was that I had to close and re-open connections once an exception occurred, but this wasn't too hard. The second gave me more trouble: The Python interface to SQLite responds to concurrent accesses by throwing an exception. So if some transaction is in progress and another thread tries to start one, that thread fails. Obviously that's not acceptable, so I had to somehow handle the exception and retry the "failed" transaction. For some reason I got inspired by the Ethernet protocol and the idea of collision handling by exponential backoff. I added a pinch of randomness and limited the maximum timeout to two seconds after lots of performance experiments, but that's what I did. Yes, it may seem like a dumb idea in retrospect, but of course it didn't seem all that dumb at the time: I didn't have much experience with Python threads, I needed to get the application done, and all this actually worked. Amazing. :-D Back to my lecture epiphany: General producer-consumer systems assume n producers and m consumers, but what do we get for m=1? A beautiful special case that solves my problem: If only one thread talks to the database, I only ever need a single database connection. If only one thread talks to the database, all transactions will be completely serialized and there will never be an exception due to concurrent access. Perfect! Of course this seemed too good to be true, so I didn't really believe I had seen all of the issues yet. Yesterday I finally had the time to re-implement the concurrency handling using a producer-consumer model. And guess what? Learning all I had to about threads was a breeze, the code is less complicated than the previous version, and the whole thing performs better too. Lessons? First, lecturers are people too, so we make bad decisions all the time. Second, think carefully about concurrency issues before you start hacking your next web application. Third, don't be afraid to re-factor an essential part of your application. Fourth, don't get too attached to cute ideas, once you have a better albeit more bland approach, throw out the cute one. And finally, learn what I don't seem to be able to: how to write concise blog posts. :-D... Read More >
  • Followup meeting next week same time and place
    from touchy by roseburt
    Thanks to those who came to the meeting yesterday, we went through and came up with content for the grant proposal. Also, we came up with a group name: TOHI [Touch Oriented Human Interface] We tried to make 'touchy' an acronym but we ended up with too much redundancy so it got shortened as above. Next meeting Weds 11/11/09 4PM DMC Game Lab -- we'll go over the grant proposal draft, make any last edits and get it ready to submit. Hope to see you then!... Read More >
  • Meeting this Wednesday Nov 4th, 4PM in the DMC Game Lab
    from touchy by roseburt
    Meeting as noted in the subject, we're going to get into the grant writing more, pin down some details and get some content written down.... Read More >
  • tentative budget info (old, needs to be updated)
    from touchy by roseburt
    here's what milt has for the beginnings of a budget: (most of this comes from the link http://arbi.trario.us/2008/11/02/diy-laser-multi-touch-table/ parts sourced from aixiz.com) the known ($44 total) for the IR lasers: 4 x "780nm 25mw laser modules" @ $8 per 4 x "89° line generator lenses that fit the laser modules" @ $3 per (this is getting pricey! ;) the unknown but probably cheap: Power supply - probably $10 wiring/resistors - cheap or we have camera with IR filter - we have wiimotes, for more resolution we could slap a filter on one of the mini HD cameras. That's all you need for the core of the touch stuff, the rest is for a dedicated surface/enclosure: Acrylic touch surface @ ? any sufficiently large Acrylic slab that doesn't bend, shouldn't be expensive rear projection applique - CAN be expensive, but we can totally find a cheap way to do it. My coworker suggested sandblasting one side of a thick piece of acrylic with almond skins (?!?) instead of an applique, he has the gear to do it. frame/enclosure - varies depending on design, we can probably easily cheap out on this part For installation/mobile use, things get a bit more complicated: mirror/prism mountings for the lasers so the light plane can be projected sufficiently close to the surface - might be pricey, might not. a large rear-projection screen or improvised solution with translucent plastic To build a working demo I would be surprised if it is more than $100. other things I think we might want to spec, so that the project can be a standalone unit: computer projector camera (and IR filter) ... Read More >

  • from touchy by roseburt
  • Meeting tomorrow (Oct 28) 4PM @ DMC
    from touchy by roseburt
    First group meeting will be tomorrow Weds Oct 28th at 4PM at the DMC (in the game lab). Planning will commence!... Read More >
  • background
    from urchon by saya
    made the background more 2-d to better complement the stick-figure pieces... Read More >
  • Asheesh's insecurities
    from gooftroop by chris
    Asheesh: "I see parts of myself in Karen." Chris: "What parts of you do you see in Karen?"... Read More >
  • We're on the wrong side of the tracks
    from gooftroop by chris
    Kendra: "I want her boobs." Katie: "Wait, what?" Kendra: "Boots. I want her boots."... Read More >
  • Meet Sparcacus
    from phf by phf
    Good news if you're thinking of taking my compilers course next time I teach it: We have yet another new member in the machine park! Meet Sparcacus, an old Sun box with SPARC processors (yes, plural!). Once again my pal uber came through for all of us by giving me this box as a present! Thanks dude! :-D This means the next compilers course will force you to generate code for either MIPS, PowerPC, or SPARC. Oh glorious future that is found in our RISCy past. :-D PS: Sparcacus is none of the Sun boxes I had gotten my fingers on previously, all of those could not be made to work. So now I have plenty of Sun hardware left over for people to pick up. Just email me if you're interested. Update 2009/11/05: I've had some trouble with the disk in Sparcy, so now I got a hold of a few more disks. I'll have to try those and see if I can find enough working disks to build a little RAID. But as of right now, Sparcy is a bit... Unstable? :-(... Read More >

Log In



New User?

Register now for an account.