Tuesday, Jan 19th, 2010 by
admin
Category:
Internet,
Programming,
Python
The recent server benchmark posted here comparing Erlang, Haskell, and Python understandably upset many people. Today we will move away from polarizing benchmarks and more about features.
Even though Erlang may not be the fastest language, it still has at least one great feature: hot swapping code without restarting the server. This is something that every long running server should have, but unfortunately it comes at the cost of performance, and some languages simply are not built to support it.
However, here is a way to emulate it for a web server, thanks to techniques borrowed from Nginx.
Here are the steps:
- Use the spawn family of system calls (ex. spawnl spawnvp) with the flag P_NOWAIT to execute itself. This new process has access to the open sockets.
- The new process should open a predetermined file containing the file descriptors of the sockets it needs to use saved by the old process.
- Start calling “accept” on the same listening sockets that the old process was accepting.
- Signal the old process to stop accepting, and to quit when all requests are finished.
While Python has a rudimentary reload, it may still be preferable to use this method as it greatly simplifies the upgrade logic. Upgrading functions piecewise may lead to corruption of logic and data.
Here is an example in Python that reloads itself in a loop using this method. It saves the file descriptors for 2 connected sockets which allows the new process to signal the old one at will. Simply run the program, and then edit the print statements below to watch it change.
import os
import time
import socket
import sys
import fcntl
import _multiprocessing
from multiprocessing import Pipe
# global pipe variable
pipe = None
spare_pipe = None
def PipeFromFD(fd):
return _multiprocessing.Connection(fd)
def recordPipe(p1, p2):
with open('fd.txt', 'w') as f:
f.write('%s,%s' % (p1.fileno(), p2.fileno()))
def upgrade():
os.spawnlp(os.P_NOWAIT, 'python', 'python', sys.argv[0], '-upgrade')
def upgradeBootstrap():
global pipe, spare_pipe
with open('fd.txt', 'r') as f:
p1, p2 = (int(fd) for fd in f.read().split(','))
pipe = PipeFromFD(p1)
spare_pipe = PipeFromFD(p2)
recordPipe(spare_pipe, pipe)
pipe.send("I am upgraded!")
if __name__ == '__main__':
if '-upgrade' in sys.argv:
upgradeBootstrap()
else:
pipe, spare_pipe = Pipe()
recordPipe(spare_pipe, pipe)
pid = os.getpid()
print '%i: Version 1. Upgrade in 5 seconds.' % pid
time.sleep(5)
print '%i: Upgrading' % pid
upgrade()
print '%i: Message from new process: %s' % (pid, pipe.recv())
print '%i: Upgrade Complete. Exiting' % pid
Example Output
user@test:/mnt/shared$ python upgrade.py
22930: Version 1. Upgrade in 10 seconds.
22930: Upgrading
22971: Version 2. Upgrade in 10 seconds.
22930: Message from new process: I am upgraded!
22930: Upgrade Complete. Exiting
user@test:/mnt/shared$ 22971: Upgrading
23025: Version 3. Upgrade in 10 seconds.
22971: Message from new process: I am upgraded!
22971: Upgrade Complete. Exiting
killall python
Saturday, Jan 16th, 2010 by
admin
Category:
Internet,
Programming Tags:
benchmark,
statistics,
web
Ever since Intel and AMD have been selling multi-core cpus, the Erlang hype has been growing continuously. The number of high profile projects using Erlang were flagrantly announced over the blogosphere as the coming of the second C.
We kept hearing about: rabbitmq, couchdb, nearly all of Amazon’s AWS, Heroku’s routing grid, Facebook chat, all switching over to Erlang because it was the fast and concurrent language of the future. No other language could hold a candle to a language run by telecom giant Ericsson which was then validated by Amazon and Facebook! Or not? When I tried to find benchmarks for Erlang, they all showed otherwise.
http://muharem.wordpress.com/2007/07/31/erlang-vs-stackless-python-a-first-benchmark/
http://www.joeandmotorboat.com/2009/01/03/nginx-vs-yaws-vs-mochiweb-web-server-performance-deathmatch-part-2/
Since I initially bought into the hype, I felt compelled to test it myself to see if they made a mistake. To do this, I wrote a simple http server in Erlang, Haskell, and Python that simply outputs an HTTP reply “Pong!”. And here are the results in a graph.

The green line is the maximum req/sec possible. Higher is better.
For more details, continue reading.
Read the rest of this entry »
Sunday, Jan 3rd, 2010 by
admin
Category:
Economics
Many Republicans believe in Reagan’s trickle down theory. They believe that by offering businesses a tax break, they will use the money to hire more people.
You don’t improve the economy by paying people to sit around and not work. You improve the economy by lowering taxes so small businesses will create more jobs.
- Representative John Linder Georgia Republican
How this frequently parroted sentiment is so popular is understandable, but… does anyone else see the problem with this argument?
If you give businesses tax breaks, they are going to horde it because they know that consumers aren’t going to have any money for them to earn.
However, if you give consumers and families a tax break, it encourages businesses to produce more, likely hiring more people in the process because that’s where the money is. Individuals and families also pay a higher tax rate than corporations that like to:
- Deduct everything as a business expense or low capital gains tax. Like personal business laptops and cars.
- Hide it in tax haven states or countries like Ikea and Walmart
- Use it to boost company stock which can be cashed out at a rock-bottom 15% capital gains tax, the same tax rate as someone earning a paltry $8,026-32,550 in 2008.

Potential Rebuttals
- But individuals will also horde money!
Read the rest of this entry »
Wednesday, Dec 16th, 2009 by
admin
Category:
Economics
People hate taxes, and everyone can agree to disagree on them. One thing most people agree on though is that it’s too complicated.
Here is a diagram of the US income tax in 2008.

US Income Tax 2008
What’s wrong with the current piecemeal tax rate?
- It’s complicated.
- People want to earn less if taking a raise barely puts them in the next tax bracket.
- The tax rate increases faster for people earning very little compared to people earning a lot.
What’s wrong with a flat tax?
- The tax rate increases more slowly as your ability to earn more money increases exponentially.
- The more money you have, the easier it is to get bargains, higher interest rates on bank accounts, and investment opportunities that allow you to own controlling shares of the next big company.
- A 20% tax of someone earning $20,000 a year can cause starving and homelessness while 20% of $10 million doesn’t really make a dent.
How do we fix this?
Read the rest of this entry »
Wednesday, Dec 16th, 2009 by
admin
Category:
Codexon
The contact form for this website has finally been fixed!
For some reason the Wordpress mail sending function refused to work.
All thanks go to this handy plugin which uses SMTP instead of the PHP function mail.
http://www.codexon.com/contact