Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What Python can learn from Erlang?

What Python can learn from Erlang?

What can we learn from Erlang for building reliable high concurrency services? This talk will shows some techniques used in Erlang and how they can be used to solve problems in a more efficient way in Python. It will also discuss how Python could evolve accordingly.
Abstract As the author of many applications and libraries in Erlang and Python I often switch from one to the other during the day. The usage of Erlang definitely changed the way I am coding in Python.

This talk will cover some techniques that are used in Erlang and other
functional programming languages and how they can be used to solve problems in more performant, robust and/or concise ways than the standard practices in Python. It will also discuss some possible changes to Python and how such changes could improve its usage by the community.

Benoit Chesneau

April 10, 2015
Tweet

More Decks by Benoit Chesneau

Other Decks in Programming

Transcript

  1. OOP to me means only messaging, local retention and protection

    and hiding of state-process, and extreme late- binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them. Alan Kay http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en
  2. • Everything run in 1 thread • GIL • Use

    OS processes (multiprocessing, gunicorn, …) • PyParallel ? • PyPy STM ? Process isolation is hard in Python
  3. • Fail fast • Crash early • Don’t catch too

    much • Crash in a way it can recover fast Let it crash
  4. • For one input we will always get the same

    output • An expression can be replaced b its value: F(I) = V, V = F(I) Immutability
  5. • Easier to think about it • Easy to test

    • Easy to share even between different processes • Thread-Safe Immutability
  6. Immutability • real world is mutable • but at some

    time, each mutation is immutable • Need to reference at some point the value of a data (identity) • snapshots can easily be stored
  7. Identity dev1 = Person("student", 23) dev1.occupation = "developer" dev1.age =

    28 developers = dict( dev1 = dev1, ... devN = devN)
  8. immutable data structures • dict should also be immutable •

    Funktown: https://github.com/zhemao/funktown • Pysistence https://pythonhosted.org/pysistence • fn.py https://github.com/kachayev/fn.py • Need more.
  9. immutable data structures • dict should also be immutable •

    Funktown: https://github.com/zhemao/funktown • Pysistence https://pythonhosted.org/pysistence • fn.py https://github.com/kachayev/fn.py • Need more.
  10. try…except but not so much • Try to fail fast,

    do not catch everything • reraise once logged • Pattern-Matching
  11. Pattern matching in Erlan {ok, Socket} = connect() {error, Error}

    will crash receive {say, Msg} -> say(Msg); quit -> quit(); _ -> % other value will crash throw(Error); end case msg() of {say, Msg} -> say(Msg); quit -> quit() % other value will crash end
  12. patterns from patterns import patterns, Mismatch @patterns def factorial(): if

    0: 1 if n is int: n * factorial(n-1) if []: [] if [x] + xs: [factorial(x)] + factorial(xs) if {'n': n, 'f': f}: f(factorial(n)) assert factorial(0) == 1 assert factorial(5) == 120 assert factorial([3,4,2]) == [6, 24, 2] assert factorial({'n': [5, 1], 'f': sum}) == 121 factorial('hello') # raises Mismatch https://github.com/Suor/patterns
  13. separate actions from decision class Action(object): ... class SendMessage(Action): ...

    class Log(Action): ... def send_message(to, msg): if is_exists(to): SendMessage(to, msg) else: Log("error %s not found" % to) run(): for action in actions: action.do()