Pycon 2012 What Python can learn from Java
-
Upload
jbellis -
Category
Technology
-
view
5.170 -
download
1
description
Transcript of Pycon 2012 What Python can learn from Java
![Page 1: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/1.jpg)
Pycon 2012
What Python can learn from JavaJonathan Ellis / @spyced
![Page 2: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/2.jpg)
![Page 3: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/3.jpg)
(Not a web development perspective)
![Page 4: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/4.jpg)
The power of tools
![Page 5: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/5.jpg)
Remote debugging
✤ -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044
✤ 11 steps: “(6) install Wing IDE on the machine on which you plan to run your debug program... (8) copy wingdbstub.py into the same directory as your source files and import it in your Python source ... (10) In wingdbstub.py on your debug host, set kWingHostPort ... try setting kLogFile variable in wingdbstub.py for log additional diagnostic information.”
![Page 6: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/6.jpg)
JMX
![Page 7: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/7.jpg)
Triton
![Page 8: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/8.jpg)
![Page 9: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/9.jpg)
GC in a nutshell
✤ Mark✤ Sweep✤ Compact
![Page 10: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/10.jpg)
Aside: reference counting
✤ Only good when allocation + assignment are relatively infrequent operations
![Page 11: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/11.jpg)
In short
✤ Python has a terrible GC story✤ Poor instrumentation makes it worse
![Page 12: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/12.jpg)
Troubleshooting OOM
![Page 13: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/13.jpg)
Runtime profiling
✤ https://github.com/foursquare/heapaudit✤ https://github.com/mariusaeriksen/heapster✤ Commercial: AppDynamics, DynaTrace, others
![Page 15: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/15.jpg)
heapy http://guppy-pe.sourceforge.net/
![Page 16: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/16.jpg)
Collections
✤ Less one-size-fits-all the more you care about performance✤ HashMap✤ ImmutableMap✤ ImmutableSortedMap✤ TreeMap✤ ConcurrentSkipListMap✤ ConcurrentLinkedHashMap✤ SnapTreeMap✤ NonBlockingHashMap
✤ Not to mention BiMap, Multimap, ...
![Page 17: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/17.jpg)
Lists?
✤ ArrayList✤ CopyOnWriteArrayList✤ ArrayBlockingDeque✤ LinkedBlockingDeque✤ PriorityQueue, PriorityBlockingQueue✤ SynchronousQueue✤ TransferQueue (Java7)
![Page 18: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/18.jpg)
Where are these in Python?
✤ Python developers want to write Python, not C✤ Similar question: “Why not Cassandra in Python?”✤ PyPy to the rescue?
✤ RPython extension methods
![Page 19: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/19.jpg)
Growing a language (1998)
✤ Guy Steele: “I should not design a small language, and I should not design a large one. I need to design a language that can grow.”✤ http://www.cs.virginia.edu/~evans/cs655/readings/steele.pdf
✤ Currently, Python is not a growable language
![Page 20: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/20.jpg)
Concurrency
✤ ExecutorService, ThreadPoolExecutor✤ FutureTask✤ Also: ScheduledThreadPoolExecutor
✤ ForkJoinPool
![Page 21: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/21.jpg)
Concurrency
✤ For CPU-bound applications, copies are the enemy✤ Useful but dangerous:
✤ List.subList, NavigableMap.subMap✤ Corollary: you need to support threads + shared state
✤ Twisted✤ Actor model✤ Multi-process + sysv
✤ Local and remote computation: one size does not fit all
![Page 22: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/22.jpg)
Copies are bad
✤ Copies of large things are especially bad✤ Remember fragmentation?
✤ Iterators (generators) are good✤ Java: ByteBuffer✤ Python: memoryview
![Page 23: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/23.jpg)
The GIL
✤ The GIL offers negligible help writing concurrent code✤ Consider this trivial race condition:
if d[k] == v1:d[k] = v2
![Page 24: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/24.jpg)
More subtle problems
✤ Which of these is threadsafe?✤ L.append(x)✤ x = x + 1✤ x += 1
![Page 25: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/25.jpg)
More subtle problems
✤ Which of these is threadsafe?✤ L.append(x)✤ x = x + 1✤ x += 1
✤ “The thread safety of python operations depends on the compilation of python statements into byte-codes, which is an implementation detail and should not be relied upon.”
![Page 26: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/26.jpg)
Are we stuck with explicit locks?
✤ ConcurrentMap✤ boolean replace(key, oldValue, newValue)
✤ NonBlockingHashMap✤ BlockingQueue✤ CopyOnWriteArrayList
![Page 27: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/27.jpg)
Shared state good, Mutable state bad
✤ final: most under-appreciated language feature?✤ guava: Immutable collections
✤ http://code.google.com/p/guava-libraries/✤ Persistent collections
✤ (More accurately, “What Python can learn from Haskell”)✤ http://code.google.com/p/pcollections/✤ SnapTreeMap
![Page 28: Pycon 2012 What Python can learn from Java](https://reader033.fdocuments.in/reader033/viewer/2022051513/545826bbb1af9fc15d8b49a9/html5/thumbnails/28.jpg)
Final thoughts
✤ JRuby is the most advanced Ruby implementation✤ Jython is less popular, but I’d rather write Java than C✤ Once you can write Python libraries that rival native ones
for speed, things will get much more interesting