Why I switch from
python to Go?
weng.wei@zalora.com
About Myself
•

Coding python since 2006

•

Speaker @ PyCon China 2013

•

Architect @ zalora.com

•

https://github.com/...
“Switching”
•

Before:
•
•

•

Love Python
python for all projects

After:
•

Still love python

•

python for hobby proje...
“Serious Projects”
•

Performance is critical

•

Code Quality is important
!

•

OK, mostly web projects
Performance
•

http://www.techempower.com/benchmarks/
Do you use pypy?
“benchmark could be very misleading”
Stress test on real Go API
Requests/sec:
Transfer/sec:

18071.20
1.19GB
•

Just anything

•

Can python?

•

Can nginx?
Why Go is so fast?
•

Async

•

Compiled

•

Multi-cores
Python
tornado

tornado

tornado

nginx (4 workers)

tornado
nginx <-> tornado
•

Inter-process communication is slow
•
•

memory copy

•
•

network latency

parsing

Process overhead
Python
memcached

tornado

tornado

tornado

nginx (4 workers)

tornado
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 error...
Exception Handling

https://twitter.com/yinwang0/status/417899535344472065
Python
•

All exception checked?

•

Which one will crash the process?
!

•

Dunno?
•

Service running in “unknown state” ...
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 TestPhpToJso...
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’...
Dependency Management
Python


•















•

pip install MySQL-python
•

may work, if you have the prerequisites

•

prerequisites are system wide

•

MySQL-python ...
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(...
gevent

>>> from gevent import monkey; monkey.patch_socket()!
>>> import urllib2 # it's usable from multiple greenlets now...
•

How about MySQL-python/pylibmc?

•

Do you really want monkey patch?
•

•

Explicit is better than implicit. - Zen of p...
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://b...
cool projects
!

•

GroupCache

•

Docker

•

raft

•

beego
Gotcha
•

Template support is poor

•

Coding with static Type could be tedious
•
•

•

No generic type
reflection is awful...
Python to go
Python to go
Python to go
Python to go
Upcoming SlideShare
Loading in …5
×

Python to go

5,635
-1

Published on

Published in: Technology

Python to go

  1. 1. Why I switch from python to Go? weng.wei@zalora.com
  2. 2. About Myself • Coding python since 2006 • Speaker @ PyCon China 2013 • Architect @ zalora.com • https://github.com/Wuvist ! • Switching to Go last year
  3. 3. “Switching” • Before: • • • Love Python python for all projects After: • Still love python • python for hobby projects • go for serious projects
  4. 4. “Serious Projects” • Performance is critical • Code Quality is important ! • OK, mostly web projects
  5. 5. Performance
  6. 6. • http://www.techempower.com/benchmarks/
  7. 7. Do you use pypy?
  8. 8. “benchmark could be very misleading”
  9. 9. Stress test on real Go API
  10. 10. Requests/sec: Transfer/sec: 18071.20 1.19GB
  11. 11. • Just anything • Can python? • Can nginx?
  12. 12. Why Go is so fast? • Async • Compiled • Multi-cores
  13. 13. Python tornado tornado tornado nginx (4 workers) tornado
  14. 14. nginx <-> tornado • Inter-process communication is slow • • memory copy • • network latency parsing Process overhead
  15. 15. Python memcached tornado tornado tornado nginx (4 workers) tornado
  16. 16. Go Go binary with GroupCache https://github.com/golang/groupcache
  17. 17. Performance • Care abut performance? You must consider go • http://blog.cloudflare.com/go-at-cloudflare
  18. 18. Code Quality
  19. 19. Error • Python: scripting language • • deal with run-time errors Go: compiled language • deal with compile-time errors
  20. 20. Exception Handling https://twitter.com/yinwang0/status/417899535344472065
  21. 21. 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!
  22. 22. Exception in Go • error • • Must check panic • Crash
  23. 23. Hacky • Go doesn’t enforce error checking • Go enforce all variable usage
  24. 24. ➜ ~GOPATH git:(feature/rpccache) ✗ go run h.go # command-line-arguments ./h.go:8: err declared and not used
  25. 25. Test • Python • • Are you actually using pyunit? Go • go test
  26. 26. https://github.com/Wuvist/gophpserialize
  27. 27. https://github.com/Wuvist/gophpserialize/blob/master/ gophpserialize_test.go
  28. 28. ➜ 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
  29. 29. Performance Tuning • Python: • from timeit import Timer ?! • import cProfile ?
  30. 30. Go pprof • http://blog.golang.org/profiling-go-programs
  31. 31. Productivity
  32. 32. • 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
  33. 33. Dependency Management Python
 • 
 
 
 
 

  34. 34. • pip install MySQL-python • may work, if you have the prerequisites • prerequisites are system wide • MySQL-python is system wide • So, vitualenv?
  35. 35. go • go get = pip (kind of) • gopath = per project virtualenv ! • Both are built-in tools
  36. 36. Deployment • Deploy python(Django?) project • WSGI, FastCGI, SCGI, or AJP? • python version? • dependencies?
  37. 37. GO • Copy & run the binary
  38. 38. Concurrency • Python • twisted • gevent • not as node.js’s callback hell, but…
  39. 39. 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
  40. 40. 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
  41. 41. • 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
  42. 42. why not just go?
  43. 43. Q&A
  44. 44. Go tips • Start with Go's official libs • Look through their source code • Consider use/create a framework • http://beego.me
  45. 45. cool projects ! • GroupCache • Docker • raft • beego
  46. 46. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×