發表文章

SQL Job Queue的寫法

最近網站要Deploy到MySQL上才發現之前寫得很多都有問題或是效率不好。 之前對於Job Queue的寫法是這樣寫: def pop(): stamp = time() db.query(Table).update({'stamp': stamp}) return db.query(Table).filter_by(stamp=stamp).all() 後來發現原來MySQL有SELECT .. FOR UPDATE的用法(是不是該修一修Database了XD) 加上了FOR UPDATE後,目前被選到的row就會被Lock起來。而其他的Process如果選到了 同樣的row,就必需等到commit後才能繼續。 所以就可以寫成: def pop(): records = db.query(Table).with_lockmode('update').filter(Table.status == 'NEW').all() db.query(Table).filter(Table.id.in_([x.id for x in records])).update('status': 'QUEUED') db.commit() return records 由於我們把status設成了'QUEUED',而在select時選擇Status為'NEW'的row,所以並不會造成conflict或是lock wait。 這樣的寫法好多了,而且在MySQL裡也比較不會出問題。

Script for notifying process termination

圖片
When I'm working, I usually have a lot of terminal open with different tasks running in different terminal. When I run a process that takes a long time(e.g. compiling, file prcoessing), I usually switch to another terminal. The annoying thing is that I need to constantly switch back to the terminal to check the progress of the process. Because of this, I wrote a script that monitors processes specified, and notify you when the process is finished via 'notify-send'.

Show GIT status on bash prompt

圖片
It's been a while since I last post, so I'm quite surprised by the new blogger interface. Guess it's all because of Google+ integration :p I've been using git for quite a while, and it's by far the best VCS I've ever used. The thing is, when you have a lot of branches in you project,it's really hard to keep track of what branch you are on. It would be nice to have a indicator or something that tell you just that. I've seem people using Fish shell . With Fish shell, you can simply write a ruby script to customize the prompt text, showing git status is just one of the things you can do. But since I never get used to Fish shell and at that time my projects never went over two branches, I didn't think it's necessary. Recently I am working on some projects that requires 3 to 4 branches, so I sometimes commit/edit on the wrong branch. That's when I decided to add git status to my prompt.

Turning off discrete graphics for Acer Aspire 4750G

OPTIMUS on Linux has always been a pain in the ass for Linux users. It drains your battery power but you can't use it! Here is a way to turn it off on Linux, enjoy. Install acpi_call From github git clone https://github.com/mkottman/acpi_call.git cd acpi_call make sudo cp acpi_call.ko /lib/modules/`uname -r`/kernel/drivers/acpi From AUR If you are using Arch Linux, it's available at here

Embedded System Final Project

圖片
Embedded System Fianl Project, utilizing 1. Qt-Embedded 2. libchewing (big thanks to Jserv) 3. SQLite 4. QtWebkit 5. ibus-boshiamy Sources can be found at github Here's a video which I demonstrated on our final project meeting. (Sorry for the bad quality)