Date post: | 06-May-2015 |
Category: |
Technology |
Upload: | weng-wei |
View: | 4,810 times |
Download: | 0 times |
About Myself• Coding python since 2006
• Speaker @ PyCon China 2013
• Architect @ zalora.com
• https://github.com/Wuvist
!
• Switching to Go last year
“Switching”• Before:
• Love Python
• python for all projects
• After:
• Still love python
• python for hobby projects
• go for serious projects
“Serious Projects”
• Performance is critical
• Code Quality is important
!
• OK, mostly web projects
Performance
Do you use pypy?
“benchmark could be very misleading”
Stress test on real Go API
Requests/sec: 18071.20 Transfer/sec: 1.19GB
• Just anything
• Can python?
• Can nginx?
Why Go is so fast?
• Async
• Compiled
• Multi-cores
Python
nginx (4 workers)
tornado tornadotornadotornado
nginx <-> tornado
• Inter-process communication is slow
• network latency
• memory copy
• parsing
• Process overhead
Python
nginx (4 workers)
tornado tornadotornadotornado
memcached
Go
Go binary with GroupCache
https://github.com/golang/groupcache
Performance
• Care abut performance? You must consider go
• http://blog.cloudflare.com/go-at-cloudflare
Code Quality
Error
• Python: scripting language
• deal with run-time errors
• Go: compiled language
• deal with compile-time errors
Exception Handling
https://twitter.com/yinwang0/status/417899535344472065
Python• All exception checked?
• Which one will crash the process?
!
• Dunno?
• Service running in “unknown state” is the worse thing could happen on production server!
Exception in Go
• error
• Must check
• panic
• Crash
Hacky
• Go doesn’t enforce error checking
• Go enforce all variable usage
➜ ~GOPATH git:(feature/rpccache) ✗ go run h.go # command-line-arguments ./h.go:8: err declared and not used
Test
• Python
• Are you actually using pyunit?
• Go
• go test
https://github.com/Wuvist/gophpserialize
https://github.com/Wuvist/gophpserialize/blob/master/gophpserialize_test.go
➜ gophpserialize git:(master) go test -v === RUN TestUnmarshal --- PASS: TestUnmarshal (0.00 seconds) === RUN TestPhpToJson --- PASS: TestPhpToJson (0.00 seconds) === RUN TestPhpToJson2 --- PASS: TestPhpToJson2 (0.00 seconds) === RUN TestPhpToJsonComplex --- PASS: TestPhpToJsonComplex (0.00 seconds) PASS ok _/Users/wuvist/source/gophpserialize 0.021s
Performance Tuning
• Python:
• from timeit import Timer ?!
• import cProfile ?
Go pprof
• http://blog.golang.org/profiling-go-programs
Productivity
• go is just like python
• easy
• concise
• Yes, go needs more code than python
• Usually exception checking
• Don’t really need to trade productivity for performance & code quality
Dependency Management
• Python
• pip install MySQL-python
• may work, if you have the prerequisites
• prerequisites are system wide
• MySQL-python is system wide
• So, vitualenv?
go
• go get = pip (kind of)
• gopath = per project virtualenv
!
• Both are built-in tools
Deployment
• Deploy python(Django?) project
• WSGI, FastCGI, SCGI, or AJP?
• python version?
• dependencies?
GO
• Copy & run the binary
Concurrency
• Python
• twisted
• gevent
• not as node.js’s callback hell, but…
Tornado
@gen.coroutine!def fetch_json(url):! response = yield AsyncHTTPClient().fetch(url)! raise gen.Return(json_decode(response.body))!
http://www.tornadoweb.org/en/stable/gen.html#tornado.gen.coroutine
gevent
>>> from gevent import monkey; monkey.patch_socket()!>>> import urllib2 # it's usable from multiple greenlets now
http://www.gevent.org/intro.html#monkey-patching
• How about MySQL-python/pylibmc?
• Do you really want monkey patch?
• Explicit is better than implicit. - Zen of python?
• Or wait for python 3.4?
• beta 3, 26 January 2014
why not just go?
Q & A
Go tips
• Start with Go's official libs
• Look through their source code
• Consider use/create a framework
• http://beego.me
cool projects
!
• GroupCache
• Docker
• raft
• beego
Gotcha• Template support is poor
• Coding with static Type could be tedious
• No generic type
• reflection is awful
• JSON (parsing) is slow! Much slow than python
• https://code.google.com/p/go/issues/detail?id=5683
• Only fields with capital letters are exported