Monthly Archives: March 2013

[python] Improve performance of application-logging ( Part 1 – server side)

Everybody knows that logging something on screen or to a file is inevitable when you create program. It makes thing visible to you , easy control and for debugging also.

But recently what I have faced is that the performance of my application ( a bunch of services written in python) has decreased so much when I try to print out so many thing to the console ( the primary I/O channel I have ever used).

So I’m finding some ways to make this process become better.

About the context, these log is actually not realtime-critical. I want want to know which step my program is at and what is the result of the executing. That’s all.

The first testing method I try to implement is using UDP broadcasting . Sound so much insanity , right ? I just want to separate the logging process to other task. Not on my main task. And also, that logging should be flexible enough that I can choose to write on the console, write events to database or to the file without any performance-losing from my main jobs.

I have already installed twisted on my machine. So I will take advantage of it.

Let’s build a UDP Event logger server, just modify the class name of UDPEchoServer example.

We have:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from pprint import pprint
class UDPLogger(DatagramProtocol):
 def datagramReceived(self, datagram, address):
 print 'signal from '
 pprint(address)
 print 'content of msg = ' + repr(datagram)
 self.transport.write(datagram, address)
def main():
 reactor.listenUDP(8000, UDPLogger())
 reactor.run()
if __name__ == '__main__':
 main()

[web.py] Rendering html template.

Moving to this part http://webpy.org/docs/0.3/tutorial#templating

The basic idea of the template things here is just like what I used to work with ASP.NET years ago. 
It’s a mix of python syntax with html tags to insert properly data into html templates.

For example, the given example from tutorial.

$def with (name)

$if name:
    I just wanted to say <em>hello</em> to $name.
$else:
    <em>Hello</em>, world!

    While the $def with(name) is kinda function declaration for html template where you can define the expected parameters that you want to pass into in the future use.
    The rest of this is in python-like syntax, with the $ sign to denote that this is python line of code mixed into html syntax.

They also noticed that we have 2 ways of using variable in this mode, if you use variable like $name , when you pass into some sort of escape html syntax ( like <em>MyName</em>, it will escape your text properly to render in browser. If you want to display exactly what you’re passing into, you may want to try $:name . That’s it. 

Next , we will find out how to deploy this template into our web app.

Let’s move back to our code.py  

We will declare a global variable name renderer

web.template.render('template/')

We got/template’ is the directory ( in a relative direction with our current main function code i.e, code.py)

Next, in our GET method, we will use this renderer to represent data onto the html template.

name = 'Toan'
return render.index(name)

[web.py] How to run our web app.

Ahh, I forget to mention about this.

In development mode ( or debug mode), we can run by submit the comand:

python code.py

This will start our app on debug-built-in server at : 

http://0.0.0.0:8080/   

Or we are also able to specify which port we want to run at by adding the port number.

python code.py  1234

Ola, done.

 

 


[web.py] Piece#1 – Getting my hand dirty with some code.

I’m starting with the official tutorial on webpy.org site, which can be found at http://webpy.org/docs/0.3/tutorial 

They introduce a little bit about URL, which results at the using of it in code by using a list of mapping URL, something like:

url = (

 ‘/’ , ‘index’
)

The first part, ‘/’ ( slash) is the regex ( regular express) that is used to represent the URL. Here we got a slash only , that mean the root directory of our web app.  And text “index” is the name of class ( or ‘Handler’ class )  that will handle the request to this corresponding URL(s). URL in plural because we may have multiple urls that qualifies the regex definition here.

Next, we need to define the ‘hander’ class called ‘index’ that we mentioned above.

class index:

def GET(self):

               return “Hello GET request”

And we can start our very first app now by:

if __name__ == “__main__”:
          app = web.application(url, globals())
          app.run()

Ola, we’re done with the very simple app.

 


[web.py] Getting familiar with web.py framework

My project recently has come up with the requirement to implement a gateway for mobile app. What we already have had is actually a great back-end service for doing “big things”. We have adopted Twisted framework for these big jobs, but I need something simple enough and must be portable to run on different platform without any significant trouble.

So I decide to abandon Twisted to move to the web.py framework ,which I consider to be simple enough in idea and function. And the most important thing is that it’s still very “python”, enough for me to bring it to different convenient platform like cloud-services ( Google AppEngine .. etc).

I found it rather simple to learn about this new framework, but to make it more memorable, I decided to blog my learning process so that I can return to this later, maybe I will forget to read about something important which I don’t already know about this framework.

Let’s get started !!!!


[Git] How to undo pending changes on local repo.

git reset HEAD <file name | pattern of file names>