- By Edmond Lau
- Highly Recommended 👍
- http://www.theeffectiveengineer.com/
- They are the people who get things done. Effective Engineers produce results.
| package main | |
| import ( | |
| "context" | |
| "flag" | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "os" | |
| "os/signal" |
Asynchronous programming can be tricky for beginners, therefore I think it's useful to iron some basic concepts to avoid common pitfalls.
For an explanation about generic asynchronous programming, I recommend you one of the [many][2] [resources][3] [online][4].
I will focus on solely on asynchronous programming in [Tornado][1]. From Tornado's homepage:
| --type-set=coffee=.coffee | |
| --type-set=haml=.haml | |
| --type-set=sass=.sass | |
| --type-set=minifiedjs=.min.js | |
| --nocss | |
| --nominifiedjs | |
| --ignore-dir=coverage | |
| --ignore-dir=dragonfly | |
| --ignore-dir=javascripts/lib |
Wensheng Wang, 10/1/11
Source: http://blog.wensheng.org/2011/10/performance-of-flask-tornado-gevent-and.html
When choosing a web framework, I pretty much have eyes set on Tornado. But I heard good things about Flask and Gevent. So I tested the performance of each and combinations of the three. I chose something just a little more advanced than a "Hello World" program to write - one that use templates. Here are the codes:
| # Copyright (c) 2010, Philip Plante of EndlessPaths.com | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in |
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # | |
| # Copyright 2012 Ethan Zhang<http://github.com/Ethan-Zhang> | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | |
| # not use this file except in compliance with the License. You may obtain | |
| # a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| import sys | |
| from gevent import server | |
| from gevent.baseserver import _tcp_listener | |
| from gevent import pywsgi | |
| from gevent.monkey import patch_all; patch_all() | |
| from multiprocessing import Process, current_process, cpu_count | |
| def hello_world(env, start_response): | |
| if env['PATH_INFO'] == '/': | |
| start_response('200 OK', [('Content-Type', 'text/html')]) |
| # -*- coding: utf-8 -*- | |
| # 2.如遇到 UnicodeEncodeError: 'ascii' codec can't encode characters in position 22-25: ordinal not in range(128) | |
| # 增加如下3行 | |
| import sys | |
| reload(sys) | |
| sys.setdefaultencoding('utf-8') | |
| from pyquery import PyQuery as pq |