Sunday, March 13, 2011

Tổng quan về kinh tế học #1




Nói đến kinh tế là nói đến quá trình sản xuất, trao đổi hàng hóa, của cải vật chất và tinh thần trong xã hội. Thông qua quá trình đó, con người tương tác với nhau để tạo ra của cải vật chất hay những giá trị mới, tái phân bổ chúng, phục vụ cho các nhu cầu trong cuộc sống, rồi lại lao động và lại tái tạo ra những giá trị mới khác,... Đó là tổng thể của cái gọi là nền kinh tế. Làm sao để nền kinh tế tạo ra thật nhiều giá trị? Làm sao để quá trình sản xuất và lưu thông hàng hóa hiệu quả hơn?... Đó là những vấn đề của kinh tế học.

Một trong những yếu tố cơ bản của nền kinh tế là thị trường và tiền tệ.

Nơi hàng hóa được đem ra trao đổi, mua bán được gọi là thị trường. Vd: thị trường bất động sản, thị trường chứng khoán, chợ,...

Hàng hóa có thể được trao đổi, mua bán trên thị trường dưới nhiều hình thức:
- Trao đổi trực tiếp:
   vd: 1 trâu đổi 100 thúng lúa
- Trao đổi gián tiếp (quy đổi sang một loại hàng hóa trung gian)
   vd: 1 trâu <-> 100 thúng lúa
         1 bò <-> 80 thúng lúa
         1 căn nhà <-> 1000 thúng lúa
   hoặc,
         1 trâu <-> 1 lượng vàng
         1 bò <-> 0.5 lượng vàng
         1 nhà <-> 100 lượng vàng

Yếu tố đóng vai trò trung gian trong trao đổi hàng hóa được gọi là tiền tệ. Thời xưa, tiền tệ cũng chính là hàng hóa (lúa, vàng, bạc,...), bản thân chúng có giá trị nội tại. Khi kinh tế ngày càng phát triển, nhiều hình thức tiền tệ khác ra đời làm cho quá trình trao đổi, lưu thông hàng hóa thuận tiện, nhanh chóng hơn như tiền đồng, tiền giấy, tiền điện tử,... Tiền tệ trở về với đúng bản chất của nó: làm trung gian trong trao đổi hàng hóa, bản thân nó không có hoặc có rất ít giá trị nội tại. Vai trò trung gian, đại diện cho giá trị thực đó của tiền tệ được đảm bảo bởi nhà nước và pháp luật.

Saturday, March 12, 2011

Be careful writing hooks to Rack layer (middle ware layer) in Rails

I've got this error from a Rails project:

ERROR NoMethodError: private method `split' called for 0:Fixnum


Following the link below gave me some clues:
http://rack.lighthouseapp.com/projects/22435/tickets/52-read-error-nomethoderror-private-method-split-called-for-0fixnum


1. something's wrong in Rack layer
2. someone wrote incorrect code that hooked to Rack layer 

Case #1: rarely happened
Case #2: may be 

I did some search and found this code in authentication module:
response.headers["Expires"] = 0

This must be the cause of the error. 

Though the correct code is just: 
response.headers["Expires"] = "0"

it still took me over a day to find out and fix that bug!!! 

Simple but not simple!


A case of "Broken Pipe" error in Rails

Have you ever seen "Broken PIPE" error in Rails?

It somehow like this:


*** Exception Errno::EPIPE in Passenger RequestHandler (Broken pipe) (process 22235):
    from /usr/lib/ruby/gems/1.8/gems/passenger-2.1.2/lib/phusion_passenger/rack/request_handler.rb:67:in `write'


This error seems appear when your program interacts improperly with external programs. In my case, it happened to me in a project that I used XMLRPC library to remotely communicate with some web services.

My implementation for that feature is as following:
- create a XMLRPC connection object
- use that XMLRPC object every time calling a webservice API

There some other guys had been headache with that error also:
http://gaveen.owain.org/2008/04/errnoepipe-broken-pipe-mysql-error-in.html
http://stackoverflow.com/questions/1082166/exception-errnoepipe-in-passenger-requesthandler-broken-pipe
http://stackoverflow.com/questions/4351624/ruby-on-rails-errnoepipe-broken-pipe

and the reasons they found come around Passenger or Mysql.

In my case, I've got the same thing even with upgrading Passenger or migrating to another web server.
After a lot of retries, I've found that if I call the API very often, "Broken pipe" doesn't appear, but if I stop doing anything for a while and then call the API, it does happen. That observation led me to a guess: XMLRPC connection object may be broken after a certain amount of time (timeout). Excellently, that's exactly right! I then changed my implementation so that every time calling a webservice API, I use a newly created XMLRPC connection object instead of using the same XMLRPC connection object for every API calls.

That solved my problem!

Tuesday, February 22, 2011

Django Overview


(will add more soon...)

Saturday, February 19, 2011

#1 Setup a basic Django project

, I've been getting more and more interested in Django. I think why shouldn't I write a series about it. So, let's start with the basics first: how to setup Django development environment and build a basic Django project.

1. Setup environments
  • Install Python
       Get Python at http://www.python.org
       To verify if it's already installed, at console:
   $ python
   Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
   [GCC 4.4.5] on linux2
   Type "help", "copyright", "credits" or "license" for more information.
  • Install Django
       Download a Django release at http://www.djangoproject.com/download/
   $ tar xzvf Django.tar.gz
   $ cd Django
   $ sudo python setup.py install 

       To verfiy what's your current Django's version
   $ python
   >>> import django
   >>> django.get_version()
    
2. Create a basic Django project 
  • To initialize project 
    $ django-admin.py startproject mysite
          This will create a basic Django project in mysite directory that looks as below
    mysite/
       __init__.py
       manage.py
       settings.py
       urls.py
  • Now run a development webserver to see if your Django site works
    $ python manage.py runserver
    Validating models...
    0 errors found.

    Django version 1.0, using settings 'mysite.settings'
    Development server is running at http://127.0.0.1:8000/
    Quit the server with CONTROL-C.       
  • Goto http://127.0.0.1:8000 on your Web browser, you'll see a "Welcome to Django" page. It's works!
So, you have setup a very basic Django project. If you want to make more complex stuffs, you may need to use a database backend, create some models, build your site's templates, mapping urls to what will process & response the corressponding templates (Django call this "views"), add some caching mechanisms to boost performance,...

Friday, February 18, 2011

How caching works in Django

Last time, I had a chance joining to a Django project. Django is a web framework written by Python. Though I've invested sometime before to study Django and been amazing with it, I haven't really done anything on Django yet. So that's a good opportunity for me to increase my Django knowledge. One of my tasks at start was to study caching supports in Django. Compare to what I knew with Ruby on Rails, I found that caching in Django is very strong and especially well-organized. So, it would be useful to put some summarizations down here.  
  1. General rule

    given a URL, try finding that page in the cache
    if the page is in the cache:
        return the cached page

    else:
        generate the page
        save the generated page in the cache (for next time)
        return the generated page

    1. More specific



    2. Advantages and disadvantages of caching backends

      Advantages Disadvantages
      Memcached
      - the fastest, most efficient type of cache available to Django
      - all cached data is stored directly in memory, so there’s no overhead of database or filesystem usage
      - cached data can be shared over multiple machines, so it’s excellent for scaling
      - being used by Facebook, Wikipedia,...


      - cached data is stored in memory, so it will be lost if server crashes (but it’s not critical because cached data is just temporary)
      - need to have memcached daemon along the way
      In-database cached
      - cached data is persistent
      - could use multiple databases for caching


      - setup & manage multiple caching databases would be tough
      Filesystem cached
      - cached data is persistent
      - simple to setup


      - may not scale well
      Local-memory cached
      - simple
      - not nescessary to have runing an external cache server as memcached

      - no cross-process caching means it’s not particularly memory-efficient
      - probably not a good choice for production, just nice for development


    3. Reference
      http://docs.djangoproject.com/en/1.2/topics/cache/
              

    Monday, October 25, 2010

    Quảng cáo trên Google



    Google cung cấp dịch vụ quảng cáo thông qua bộ máy tìm kiếm. 

    Có 2 nguồn thông tin mà bộ máy tìm kiếm của Google sẽ tìm khi người dùng gõ một từ khóa: 
      - thông tin của Google (do Google tự tạo ra)
      - thông tin có sẵn trên internet (do người khác tạo ra, như các trang web...) 

    Kết quả tìm kiếm cũng dc hiển thị theo 2 dạng dựa vào 2 nguồn thông tin trên.

    Do đó, chiến lược quảng cáo trên Google cũng gồm 2 hướng:

      1. Thuê Google trực tiếp đăng quảng cáo của bạn. Google sẽ đưa thông tin của bạn vào nguồn "thông tin của Google", cái này gọi là dịch vụ Google AdWords, muốn xài cái này cần phải trả tiền, trả càng nhiều tiền thì thông tin của bạn càng dc quảng bá nhiều (tần xuất xuất hiện trên kết quả tìm kiếm trong ngày, vị trí xuất hiện,...). Cái này đòi hỏi người dùng phải có khả năng thanh toán online (Paypal, VISA card, ...) để trả tiền đăng quảng cáo cho Google. 
          Có 2 khoản cần thanh toán cho Google: 
          - Chi phí quảng cáo hằng ngày (trung bình khoảng 10$ mỗi ngày)
          - Chi phí trả cho mỗi lần có ai đó click chuột vào quảng cáo (0.001$ ???)

         Tham khảo:
         http://www.youtube.com/watch?v=tx2L6EGa9DY&feature=related

      2. Điều chỉnh thông tin của bạn (trang web) để Google dễ dàng tìm thấy (Google càng dễ tìm thấy thì trang web xuất hiện càng sớm trong kết quả tìm kiếm). Hướng này gọi là SEO (Search Engine Optimization). Hướng này đòi hỏi những điều chỉnh về mặt kĩ thuật đối với trang web của bạn sao cho thân thiện với bộ máy tìm kiếm như tối ưu hóa mã nguồn trang web, tạo nhiều liên kết từ bên ngoài tới trang web của bạn...