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

Topics of Interest (Async)

David Beazley
November 10, 2015

Topics of Interest (Async)

Invited Keynote. PyCon Brasil 2015. Talk includes live-coding which is best viewed on video. Conference video at https://www.youtube.com/watch?v=kkt_BtR9Kzk. Screencast at https://www.youtube.com/watch?v=lYe8W04ERnY

David Beazley

November 10, 2015
Tweet

More Decks by David Beazley

Other Decks in Programming

Transcript

  1. Background If you need to write a program to manage

    10000 concurrent network connections, then asyncio is your problem.
  2. Async/Await in C# public static async void CopyToAsync(Stream source, Stream

    destination) { byte[] buffer = new byte[0x1000]; int numRead; while((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) > 0) { await destination.WriteAsync(buffer, 0, numRead); } } Source: http://blogs.msdn.com/b/dotnet/archive/2012/04/03/ async-in-4-5-worth-the-await.aspx
  3. Javascript (ES7) async function chainAnimationsAsync(elem, animations) { let ret =

    null; try { for(const anim of animations) { ret = await anim(elem); } } catch(e) { /* ignore and keep going */ } return ret; } Source: http://tc39.github.io/ecmascript-asyncawait/
  4. Dart import "dart:html"; main() async { var context = querySelector("canvas").context2D;

    var running = true; // Set false to stop game. while (running) { var time = await window.animationFrame; context.clearRect(0, 0, 500, 500); context.fillRect(time % 450, 20, 50, 50); } } Source: https://www.dartlang.org/articles/await-async/
  5. Scala val futureDOY: Future[Response] = WS.url("http://api.day-of-year/today").get val futureDaysLeft: Future[Response] =

    WS.url("http://api.days-left/today").get val respFut = async { val dayOfYear = await(futureDOY).body val daysLeft = await(futureDaysLeft).body Ok(s"$dayOfYear: $daysLeft days left!") } Source: http://docs.scala-lang.org/sips/pending/async.html
  6. Fortran ASYN FUNCTION NGCD(NA, NB) IA = NA IB =

    NB 1 IF (IB.NE.0) THEN ITEMP = IA IA = IB IB = AWAI MOD(ITEMP, IB) GOTO 1 END IF NGCD = IA RETURN END
  7. Fortran ASYN FUNCTION NGCD(NA, NB) IA = NA IB =

    NB 1 IF (IB.NE.0) THEN ITEMP = IA IA = IB IB = AWAI MOD(ITEMP, IB) GOTO 1 END IF NGCD = IA RETURN END (actually I'm not so sure about Fortran)
  8. asyncio is the future "Async/await is amazing, the mecca of

    working with asynchronous code in Javascript" - Thomas Hunter
  9. Reading the Source class Future: ... def __iter__(self): if not

    self.done(): yield self return self.result()
  10. Help Me!!! • I get the big picture (concurrency). But...

    • Is it a replacement for Tornado/Twisted? • Is it a layer that everyone should use? • Is it an reference implementation of an API? • Also: How do I teach this?
  11. Some History Threads Polling def handle_echo(client): while True: data =

    client.recv(10000) if not data: break client.sendall(data) while True: r, w, _ = select(readers, writers, [] for s in r: handle_read(s) for s in w: handle_write(s)
  12. Some History Threads Polling Callbacks Futures, Deferreds Generators Inlined Callbacks

    Coroutines yield from asyncio "Green threads?" stackless
  13. Some History Threads Polling Callbacks Futures, Deferreds Generators Inlined Callbacks

    Coroutines yield from asyncio async/await "Green threads?" stackless
  14. Defending asyncio • Think about everything going on here •

    It is a complicated problem • It is an important problem • But, it has a complicated history • Each part requires deep study • A lot of smart people have worked on it
  15. THOUGHT I DO NOT WANT TO THINK ABOUT CALLBACKS, FUTURES,

    TASKS, COROUTINES, TRANSPORTS, EVENT LOOPS, ETC.
  16. An Idea Maybe Python's async should be an interface not

    an implementation not a library not asyncio
  17. Questions • Can an async API exist independently of an

    async implementation? • Maybe • It seems that it should be possible • Especially if you embrace async/await
  18. It's Interesting • The design of such an API is

    interesting • What operations require "await"? • Consistency in the interface • Interconnections between parts • Can you isolate it from the runtime?
  19. It Will Be Better • APIs encourage experimentation • And

    competition • And better interoperability • APIs are better than layers
  20. That is All • Special thanks: • Yarko Tymciurak •

    PyCon Brasil Organizers • Twitter: @dabeaz • Questions!