Category: Software Engineering

Coding, Python, web development, architecture, and deployment

  • Another Personal Test

    There are 3 different things you can do to affect your weight.

    • Exercise changes
    • Dietary changes
    • Take drugs

    It is possible to take any weight management strategy and estimate percentages of effectiveness to each of these 3 categories.  You can lose weight by just exercising more, or just eating less, or doing both.

    I have tested various diets – vegetarian, meatatarian, low carb, slow carb etc. I have also tried doing more exercise.  They all met with varying amounts of success.

    So far I have not really tried very much in the way of dietary supplements or drugs for weight management.

    For the next month I’m going to try taking the EC stack to see it’s effects.  It’s 25mg Ephedrine and 200mg Caffeine taken 3 times a day.  These two compounds taken together have proven to be effective at increasing metabolism and burning fat according to scientific reports I’ve read on Examine.com.

    We’ll see how it goes.

  • Lines of Code Per Day Performance

    There are few arguments with programmers that can elicit such passionate hatred as a debate about measuring a programmer’s performance by tracking the number of lines of code they write per day.

    There are far too many variables at play which can affect a programmers ability to type code – how well they understand the problem, the language being used, familiarity with libraries, and distractions in the workplace.  Because of these extra variables it becomes unfair to compare LOC productivity between programmers or between projects.

    However I do believe it has merit as a personal metric.

    I am a believer in mastery through practice.  You deliberately practice a musical instrument by playing the same song over and over until the performance is good.  You learn your multiplication tables by practicing on 1000’s of example problems in primary school.  You master Jujitsu by repeatedly doing the same throws over and over until they become instinct.

    For mastery to occur the practice must be deliberate and focused on improvement.  You wouldn’t get better at playing piano if you just mash keys – no matter how many hours you did it for.

    Software programming is a skill like these others which takes experience to become proficient with.  For the vast majority of programmers I’ve met proficiency comes naturally through years of 9-5 coding.  I’m not happy with that level of performance for myself.

    Measuring yourself in terms of time to code a solution to a given problem is a valuable metric to have to identify what areas you need to focus on.

    For example if you took a standard example such as implement a merge sort algorithm – and tested yourself on that over time and against different languages you would be able to measure a meaningful change in your programming performance.

    You might first find that you don’t know the algorithm well enough to type it out off the top of your head and turn to Google for a description of it (a detour that takes valuable time).  If it’s a new language you may find points that the syntax throw you back to a book.  Over time if you continued to learn you could reduce the time it takes to type out a solution.

    It’s not that typing out merge sort once per week is going to be code that you would even want to keep.  Just like you don’t record your drum practice and post it up in an album on iTunes.  The point is to identify places in your process for improvement by asking could I have typed out this code better or faster if I was more familiar with the syntax, or knew of some extra libs to use that would cut down on lines of code.

    Then once you’ve perfected that song (algorithm) move onto something new.  Practice it until there is no room for improvement in your time to solve or code quality.

    I believe that practice like this should improve your personal day to day Lines of Code Per Day metric to be 2x to 5x what it would have been without the deliberate practice.

  • Why you should use Celery and Django

    Celery is something that I kept hearing about but took me a while to wrap my head around.  Why is it important? What is it good for?

    With a couple of big Django websites under my belt it has become more obvious where something like Celery fits and why it’s useful.

    The first thing you need to know is what problem Celery solves for you.  It allows you to do things asynchronously so that you can perform longer running tasks in the background and quickly return a page to website visitors.  It also provides a means for running and scheduling tasks that might have before been managed by crontab.

    It is good for things that could result in long wait times like sending emails.  In the case where the code to send an email could potentially timeout if the mail server is down then putting the send email code into a view function could result in a long frustrating wait for the user.

    The biggest issue with cron is that in a multi-server environment it is difficult to manage what is going to run where, and balance your resources.  It’s also a bit of a pain to deploy changes to the schedule without pushing configuration files to your servers.  You lose control over any load balancing and it becomes difficult to manually kick off processes.

    Celery also provides some neat methods for delaying code to run in the future.  For example, lets say you want to send an email to users who haven’t logged in to your website in the last 7 days to remind them of your awesome service.  To optimize it further you’d like the email to be sent at a time of day when there’s a good chance they’re sitting at their computer (the same time of day as their last login).  Using a cron job you’d have a script that queried the database for all users who met the criteria and then send out all those emails at once – and run it every few minutes.  The risk there is that an error in the DB query could result in spamming a lot of people with the wrong email.

    Using Celery it’s possible to schedule the code to send the email 7 days into the future.  The task code will be much simpler – check that the conditions are still valid and send just one email to the one user.  It also scales up nicer – since it’s only going to query the DB when there is a likely chance that it actually will have some work to do.

    Setting up Celery takes a bit of work.  Actually,  getting it to work is relatively easy,  deploying it to production is a bit more involved since it requires a bunch of other tools you may not be using yet.

    There are two celery tasks that need to be demonized – the Celery workers and the Celery beat (for scheduled tasks).  Celery tasks are queued up by either the celerybeat program or from scheduling them in your app.  The queue that all these are persisted into is some sort of message queue service or database.  It can be RabbitMQ, Amazon SQS, Redit, Mongo, or a Django DB.  Workers listen to the queue for things to do and pick up work when they can.

    Demonizing the workers and celerybeat can be done with Supervisord.  It simply makes sure that if the programs crash they get restarted, and that they continually run in the background.

    Monitoring what tasks are in the queue, what is running, and the results of past tasks is through an app called Flower.

    Ensuring that everything stays up and running can be handled by Monit.

    Once the infrastructure is in place having Celery really does simplify a lot of the code you write.  Simpler code make me happy.

  • Migrating from Apache to Nginx + Gunicorn

    I was never really sure why people opted to swap out the heavily tested, widely deployed Apache web server for alternatives other than those trying to eek out a little bit more performance.  Finally I found a good reason to change my setup.

    When trying to enable Celery for asynchronous tasks on my web server I ran into some unexpected errors with imports.  Eventually I tracked the problem down to mod_wsgi was compiled and running against an old version of Python rather than the newer version in the apps virtualenv.

    The bigger problem with Apache outside of any performance issues is the coupling of Apache, mod_wsgi and python.  Upgrading python becomes complex and running multiple django apps on different versions of Python is non-trivial.  The other issue I found is that Apache takes several minutes to reload which is enough time to disrupt users.

    Nginx acts as a trivial reverse proxy.  Once it’s configured and running it will probably never need to be restarted.  It directs incoming requests to a free standing python application that is running and listening at localhost:8000.  Gunicorn is used to run the django app on port 8000.  It can be run from the command-line all within the virtualenv’s copy of python and gunicorn.  This way the django app is nicely isolated and it operates without tying itself to too much of the OS.

    Making the gunicorn process a daemon so that it runs in the background and starts after a system reboot is handled by having Supervisor running.

    The real day to day benefits to getting rid of Apache is that deployments are faster and there’s less downtime when restarting the server.  It also gives more flexibility for upgrading python rather than using the system installed version.  In the end it was easier to switch to Nginx + Gunicorn + Supervisor rather than fixing the Apache config to work with Python 2.7.3

  • UFO Invader Update Coming

    main menu mockup

    The first game I did for iPhone was released back in 2011. Seems like eons ago. The game hasn’t been updated, or promoted since that initial release and yet there are still an average of 100 people playing it every day.

    I’m working with Colum on a strategy to launch a bunch of new games over the next several months starting with an update to the code for UFO Invader.

    The new update will include many of the things I’ve learned in the last 15 months from selling my apps on the store. It should be able to improve monetization, make the game more fun, and I’ll be adding a bunch of new upgrades to the game so that players have new things to collect and buy with the in game currency.

    Much of the weekend was spent just getting the app to compile again. Now that it’s running again on my iPad there’s a lot of graphics work to do to get the app working with all the retina devices that have come out since it was originally released.

    I’m excited to see how this game update does and then to move on to creating a bunch of new games in the coming months to hopefully re-launch the Halotis brand.

  • Playing With Node.js

    When exploring ways to build a good chat server there are an overwhelming number of options.

    The old way of doing things with polling the server for new messages was simply not an option but the alternatives are a little scary.  Using a SQL database seemed a bit overkill for just posting messages.

    In the python world the asychronous server options which would support either raw sockets or websockets include Tornado, Twisted, and gevent.  The other option was to try out Node.js.

    Chat servers seem to be the de-facto example app for these types of server frameworks.  Yet unfortunately there are very few full featured examples to be found.

    I initially wrote a chat server that worked with raw sockets using Twisted.  However, testing the raw socket solution proved annoying, and creating a web based admin for the chat server would require yet another service to convert the raw socket into a websocket or develop a native client app for communicating to the server.

    So I took a step back and started again with Node.js.  Node.js with socket.io proved to be quite a bit simpler to code than the raw sockets on Twisted option.  Using websockets will add a requirement on my iOS client for a library, but it makes things much easier to test through a html client.

    For the persistence backend there are also many different options.  I have an existing MySQL server that I could have used, but I was interested in trying something a bit leaner.  Both Redis and MongoDB stood out.  At the moment I’m still evaluating which backend to use.

    So it’s not quite finished enough to share the code.  I’ll put it up on github when it’s finished.

  • What to do with a Raspberry Pi

    I’m trying to think of some ideas for what to do with a Raspberry Pi.

    There’s just so many things that people have done with these little $35 computers that it is overwhelming to think of what to do with one.

    Here’s a list of ideas I’ve scoured from the web:

    • Webcam server
    • Vehicle tracking (using an add-on GPS module)
    • Streaming internet radio box
    • Vehicle Diagnostics, full OBDII logger with touch screen interface
    • Baby monitor
    • Media server by adding a couple of USB hard drives
    • Media receiver (hopefully we’ll get a port of XBMC or PLEX)
    • Video chat
    • Game emulator, running MAME (build your own arcade cabinet?)
    • Network Attached Storage setup (NAS)
    • Mini web server
    • FTP server
    • Proxy server
    • Print server
    • Firewall
    • Portable Media PC
    • Run an alarm system
    • Security webcam (with motion sensor)
    • Control garden lighting
    • Control sprinkler system
    • Wearable computer
    • HTPC for TV web browsing
    • HTPC for streaming Netfilx / Hulu etc
    • In car Computer
    • Thin client computer
    • Game server
    • IRC / chat server
    • build a cheap laptop
    • build a cheap tablet
    • create a digital photo frame
    • Asterisk VOIP server
    • PBX
    • Home automation system
    • MP3 player
    • Portable personal computer, you can use it anywhere you can find a monitor.
    • multitouch screen coffee table
    • Wall hanging screen with voice control for network pictures, weather, news and RSS feeds
    • Cyber Cafe computer
    • Video conferencing system
    • Personal weather station / logger
    • Control a light display
    • Control an LED board
    • Put it in an old mac classic or mac plus case as a general purpose computer
    • Intelligent photo frame with touch
    • Wardriving setup
    • A dedicated Synth, possibly with touch screen
    • Solar powered desktop computer
    • CNC controller
    • High tech birthday / Xmas presents
    • Backup server
    • RSS ticker
    • High tech alarm clock
    • Mini projector
    • DOSBox for games
    • Processing farm for SETI@Home
    • Cafe media player
    • Brains for Arduino setup
    • Mumble server
    • Industrial manufacturing controller
    • TOR server
    • BitTorrent seedbox
    • Family notice board
    • CD / DVD ripping device
    • Car black box with video
    • Wall mounted, interactive mood lamp
    • Robotic telescope / camera controller
    • Display photographer portfolio images
    • BitTorrent client
    • SMS gateway
    • BrewPi – beer brewing control
    • Kitchen TV/computer
    • Internet connected photo frame
    • Home monitoring
    • Digital signage
    • Personal/home/work dashboard

    So many good ideas it’s hard to choose.

  • Good Christmas Break

    It was a nice relaxing week off for christmas this year. Having friends around to host parties made everything more festive.

    The week was bitterly cold with a streak of -30 degree temperatures for the week. It made the deepfried Christmas turkey a bit harder to cook. Thankfully the cold snap is over for now and we’re back into temperatures closer to 0 for the next week.

    Went to see some friends in Edmonton yesterday and saw what was probably the most tech laden house I’ve been in. They had file servers with 96TB of disks, at least 6 TVs in the house each with a media computer on it. The workout area had a 100″ projection screen. Interesting setup that gave me some ideas for how to get things working in my house.

  • A Live Text Chat Server

    I’m facing the challenge of building a live chat server for use in a mobile app. It is an interesting bit of code to write and I’m finding myself learning a few new things to get it working.

    My first thought was to build it using websockets over HTTP but that looked too difficult to build on both the server side and client side.

    On the server side there you need to have a different kind of server. The standard Apache web server is just not going to cut it if there are 1000+ people in a realtime chat. It’s necessary to change things over to a service such as Node.js or a Python Twisted server.

    The overhead of websockets on the client side was also too high and it appears that JSON over straight sockets is the way to go.

    From a design perspective there is the issue of room size. If there are too many people talking at once it becomes difficult to follow any sort of conversation, and with too few people the community will feel dead and unresponsive. It’s important to separate people out into ‘rooms’ to avoid too much noise while not spreading them too thin so that there’s no one to talk to. It will be an interesting challenge to try and find the right balance to this.

    I’m hoping to have something functioning by next weekend.

  • A Productive Christmas Break?

    There’s just so much work to be done that I’m really going to have to strive for a very productive month of programming.

    I need to update all my apps for the new year and hopefully launch a few new ones.

    To that end I’m planning on taking some more time off work over the next couple weeks and months to push through some coding projects.

    In the pipeline now are one new website, a redesign of another website, update 7 of my iPhone Apps with improvements aimed at increasing retention and ad revenue, and making a lot of progress on my big social game.

    The social game is what has been on my mind the most and I have very high hopes that it is going to be amazing when finished. I expect I will have to hire some artists in March to help bring a level of polish I haven’t yet been able to achieve on my own.

    Will it be possible to have a productive Christmas break or will there be too many distractions?