Django through the Firewall

Once upon a time, in a galaxy far far away… Yeah you probably heard that one before too…  Still developing a Django App behind a Firewall that has to interact with Amazon RDS and Amazon Elasticache might encounter a problem or two unless you read this post.

See also: SSH Tunnel Magic

The Problem

Developing behind a Firewall.

Cannot reach services like mailgun which can be used to send emails.

The Solution

Make Django Server communicate through a Proxy that happens to be attached to a Putty SSH Tunnel.

See also: PySocks

Adjust your Django App Startup as follows:

3-1-2013 1-58-45 PM

Your actual mileage may vary… some assembly required…  Obviously I tend to make modifications where few dare to tread but then I also get the results I want.

The Test

Ran the Django App and clicked the link to send an email via mailgun and bingo !!!  It Worked !!!

 

 

 

Advertisements

Rackspace Cloud Site Faster than Godaddy Site !!!!!

Rackspace Cloud Site Faster than Godaddy Site !!!!! 

Poor Man’s Linux Service

Quick and dirty way to make your own Linux Services

Warning this may be just too simple for most people to comprehend however this does work and it works very well.

This is a very nice short bash shell script that does just one thing, it starts a background service which in this case happens to be a tornado process while capturing the PID so the service can be stopped later.

This is another nice short bash shell script that stops the previously started tornado service.

Keeping it Simple

I like simple scripts that handle complex tasks.

Keep it simple also means keeping it Agile and there is nothing more agile than a nice short simple script.

See also

http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/

I would have written a post like this one but I wanted to get back to writing some code for a change…

Just for fun !!!

So today, just for fun, I had a wild idea !!!

Windows Web Server 2008 hosting IIS 7 running Python 2.7 + Tornado + Django 1.3 doing the same thing I was able to achieve with Ubuntu + Python 2.7 + Django + wsgi + Tornado + nginx !!!

I doubt the Windows performance will match that of Ubuntu however this has been all kinds of fun !!!

See also:  Running Django on Windows (with performance tests) !!!

I was originally interested in doing Django 1.3 with Windows Web Server 2008 and IIS 7 so what I found was pretty darned cool.

Point and click installations for all this stuff was nothing less than amazing !!!  Especially for Windows !!!  WTG Microsoft !!!

What I really want is WebDav so I can share my huge pile of files with myself and only myself via the Internet and WebDav seems to do the trick…  problem is Windows Web Server 2008 doesn’t seem to know how to do WebDav so I have to improvise a bit by making this do the trick as follows:

Tornado does wsgi !!!

Now IIS 7 does Tornado !!!

Easy as 1,2,3 !!!

Oh, and this puts me one step closer to having my own Private Cloud in my home !!!

 

 

 

Why does “crappy” code not perform worse than it seems to ?

I like good code like the next person however I am wondering why people spend so much time focusing on “crappy” code patterns that don’t seem to perform worse than expected ?!?

Here’s a thought:  If you want to complain about “crappy” code then why not fix all the crappy code you become aware of including all that stuff you cannot seem to understand !!!

Or maybe just realize the fact that your idea of “crappy” code may be another man’s treasure !!!   Oh wait, someone already thought of this when the following was coined, “…one man’s trash is another man’s treasure…”. Circa. 1570’s !!!

Or to put this another way…   those who feel some coding patterns may be “crappy” might just be limiting their own ability to perceive really useful code they would surely have overlooked !!!

I am NOT in favor of truly crappy code !!!

But, I am also NOT in favor of being so close-minded that I cannot see how useful alternative coding styles may be.

Single instances of “crappy” code cannot be crappy !!!

If you have to produce 1000’s or 1,000,000’s of iterations to see a performance problem from a single use chunk of code then there is very little reason to think about any such performance problem !!!

I might be impressed with those who rail against “crappy” code who also make darned sure all the code they can see in their field of vision is not also “crappy” code !!!

Scenario #1

For instance, consider the following pattern that was recently classified as being “crappy” by an intrepid band of hearty Python coders in an company we dare not name here…

This is “crappy” !

toks = ‘one two three four five six seven eight nine ten’.split()

This is not !

toks = [‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’, ‘ten’]

The crappy version was said to be “crappy” because the split was seen as being unnecessary because the non-crappy version was said to be the desired effect.

Those who said the crappy version was crappy have probably never had to load a list from a comma delimited jungle of data they may have had to spend several days entering manually by hand otherwise they may have gotten themselves some useful experience as to why the crappy version might be somewhat less crappy after-all.

Scenario #1 Benchmarks

I like benchmarks.  I like to know when code may perform badly at run-time at scale.

The crappy version for Scenario #1 runs 5x slower than the non-crappy version for 1,000 iterations.

The crappy version for Scenario #1 runs 7.6x slower than the non-crappy version for 10,000 iterations.

The crappy version for Scenario #1 runs 7.65x slower than the non-crappy version for 100,000 iterations.

The crappy version for Scenario #1 runs 7.66x slower than the non-crappy version for 1,000,000 iterations.

Um, if you turn-off the Python GC the performance issues seem to disappear for a while !!!  Just a thought…

Scenario #1 Analysis

The real question is this:  “Was the crappy code being used enough for the comments this code pattern elicited ?”  Probably not !!!

The justification for call the crappy version truly crappy was the performance concerns and there are some rather glaring performance concerns to be sure but ONLY when the crappy version was being used 1000 times more than it actually was being used.

Those who claimed the crappy version was “crappy” had to magnify the usage pattern by a minimum of 1000 times before the crappy version’s performance data might be measurable.

I agree the crappy version would be truly crappy if it were the actual source of some kind of measurable performance issue related to the loss of revenues or some other demonstrable effect that was actually causing some kind of problem.

The problem, as I saw it, had nothing to do with how crappy the code pattern may have been because let’s face it this is crappy code if it were to be used often enough for a problem to exist.

The problem, as I saw it, was a group of people all agreeing there was a problem where no problem really existed at-all simply because in their minds they were magnifying the problem by 1000 times just to be able to catch a glimpse of some kind of problem when there was no real problem at-all.

This piece of crappy code may have a real-world non-crappy use case that could have saved someone a lot of time, given the right set of circumstances related to having to maintain a huge data set by hand that had to be loaded into a list at run-time.  The desire to make this crappy-looking code non-crappy in a use case that could NEVER be actually measured as being crappy is the problem !!!  Far more time could have been spent entering all those commas and quote marks just to make the code less crappy than the effort would have been worth.

Why any person or group of people who are supposed to be intelligent talented software engineers would claim a harmless chunk of code was harmful given the actual use-case that existed in the actual source of the context for the original question which was related to a single use of the crappy version is well beyond my ability to comprehend in real terms.

The person who raised the issue was supposed to have more than 20+ yrs programming experience !!!  He found a single reference to the crappy version in a source file that was probably being used exactly once per iteration of some larger program.  WOW !!!  Talk about yelling “FIRE” in a crowded room !!!

The people who agreed with him were even more of a mystery because these people are supposed to be among the best and the brightest at this particular unnamed company and they went along with the idea that was raised by the one guy who should have known better than to yell “FIRE” in a crowded room.

It is interesting to note, these same people who were able to inflate a potentially crappy use-case beyond the original scope are the same people who are seemingly okay with all the following truly crappy coding patterns they seem to wish to do nothing about:

  • An Eager-loading ORM that maintains and uses exactly 1 Database Cursor per Session !!!
    • Why was this NEVER changed ???

I will stop here with the analysis because I think this one point bears further analysis.

How in the world do these crappy detecting software engineers allow an Eager-loading ORM to exist in the first place ???   And the company wants this sort of thing corrected !!!

I have to wonder about the skills these crappy detecting software engineers actually possess when they cannot find a way to remove the Eager-loading ORM in the first place !!!!

Removal of the Eager-loading ORM would be easy enough, for me to accomplish, but then I can tell the difference between crappy code that is really crappy versus crappy code that only seems to be crappy.

Well You See Timmy…

Now for the moral of this tale…

People who live in glass houses always seem overly eager to throw stones when their own houses have cracks in the walls so wide everyone knows there are problems.

I have no issues with people who can see imagined problems that don’t exist so long as they have their own houses in order but this was not the case in this instance.

These very same people, who seemed more than willing to detect crappy code patterns where there was no crappy code use case are the very same people who seem unwilling or unable to resolve glaring performance issues in a huge pile of code.

The rest of the issues these people could focus on are as follows:

  • mod_python rather than wsgi
  • Django not being used-all but then there is no discernible web framework being used at-all.
  • Eager-loading ORM – easy to resolve with Django.
  • Non-scalable Web App – because mod_python is being used rather than wsgi, for instance.
  • Development environment issues – all developers share a single instance of the run-time – each developer gets a different virtual host but all development being done in a single Linux instance.
    • Okay, this one truly baffles me !!!
    • How difficult can it be to get each developer their own Linux instance at a moment in time when everyone has a Cloud-based solution for doing this ?!?

Look, all these issues can be easily handled but none of them are being handled at-all.  Why ???

The reason(s) all these glaring issues are not being handled is easy… lack of experience and lack of skill in the developer community.

Nobody wants to make any real changes or nobody is able to make any real changes.

Dragging along ancient code from the deep past and then being either afraid to update it or unwilling to update it is more than ridiculous !!!

Solutions !!!

The solution for all this is also easy but very difficult to implement !!!

Rewrite your code every 18 months !!!

Better tools are being churned-out all the time.

Django is a proven Web Framework !!!

Wsgi is a proven technology stack !!!

Python+Django+tornado+wsgi+nginx equals a scalable Web App that scales as easy as you can build an automated process for spinning-up one more Linux Virtual Machine in the Cloud !!!

Or let’s put this another way…

Python+Django+tornado+wsgi+nginx was easy enough for me to handle all by my self – not that I might not have wanted to do this with a team of others – there just weren’t that many others I might have done this with.

The moment I achieved my first stable installation of Python+Django+tornado+wsgi+nginx I knew it was the way to go !!!

Python+Django runs as a separate wsgi web server with performance comparable to what you get from the Google App Engine, oddly enough, and “yes” I have run benchmarks that tell me this based on the data.

Tornado is a stand-alone Python-based Web Server with very good performance characteristics.

Tornado talks to an instance of a Python+Django web app via wsgi.

Nginx talks to an instance of Tornado that talks to an instance of a Python+Django web app via wsgi.

Why so many web servers in this stack ???

Why use Tornado at-all ???

I happen to know a little something I call Latency Decoupling that tends to make web pages serve much faster the more layers of web servers you use.

Nginx connected to many Tornado servers each connected to one or more wsgi Web Apps is far more efficient serving web content than Nginx connected directly to that very same wsgi web app.

Latency Decoupling kicks-in and your end-users have happy faces.

Ability to Scale the Web App also increases !!!

Many instance of the Web App within each Tornado instance !!!

Many Tornado instances within each Nginx instance !!!

Deployment gets easier !!!

Now with a single Python or Ant script you can spin-up yet another Amazon EC2 instance – connect-up the Nginx instances using a Load Balancer of some kind (nginx also does Load Balancing) and before you know it you have architected a really cool Django Cloud Solution that nobody else seems to have just yet.

Build a slick Control Panel for your Django Cloud Users and bingo you have the ability to grow a Web App from a single instance to any number just by clicking a button on a web page !!!

The only other detail would be how you monetize all this into something you can use to generate revenue.

All of this was easy enough to build when you have all the parts.

All of this should be easy enough for any company to use, if only they had I.T. staffers who had played around with these kinds of solutions but alas that seems to be lacking in most companies except for a few.

Too bad most would-be skilled programmers would tend to scoff at most of what’s written in this article as being some form of “crazy”… but then once upon a time the notion of generating electricity was also seen as being “crazy” along with the notion of gravity and quantum physics.  I can live with what others wish to say… so long as I get to build something really cool along the way.

Vyper Logix Corp Makes ITC (Inter-Thread Communications) Easy as 1,2,3 !!!

Take a look at the code sample found in this article !!!

Very Lean !!!

Very Cool !!!

Threaded !!!

Best of all the main process terminates itself once all the work has been done !!!

 

 

Code sample was printed using Wing IDE and Snag-It 10.

 

Python Type-Cast the Easy Way !!!

Python objects hold key and value pairs – this is what every object holds, key and value pairs.

Python objects that have a __dict__ make it easy to convert the object from one class to another simply by replacing the old object instance with another as follows:

See the attached file with a sample of how this can be done most simply. (type-cast-lessons.zip)

There are other ways to accomplish the same goals using more elaborate means however this works using less code.

What to do when your objects do not implements the magic methods required for the aforementioned code sample ?

Easy, you can use method injection as described here or here or here or here.

It also helps to use a really good Python Debugger in the form of the Wing IDE.  Stare at the debugger while debugging Python code just long enough and sooner or later you may begin to see how to accomplish goals you may find useful.

I am not recommending you try using the aforementioned code as part of any professional solutions unless  you know who will be doing a Peer Review of your code but I am saying these techniques may be useful in learning something about the object types being used as to whether or not the object types are meaningful.

The point being, you can either grouse and lament how things cannot be done or you can learn the Agile Method for getting things done…

%d bloggers like this: