Bestand wählen
Merken

Why you might want to go async

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Beta
Erkannte Entitäten
Sprachtranskript
uh yeah hi everyone i'm Ioannis uh I'm originally from switzerland however the last 4 and a half years I've been living in Tokyo Japan and I'm working for a company called each in Japan and we are hiring like everybody else so if you're looking for a job or if you're a university student we also have a fantastic internship program where we will pay your flight to and from Japan and you have a paid internship for 6 to 8 weeks with us that if you're interested in any of this uh please feel free to come talk to me or check on the website
uh just click links where to find me if you have any complaints or feedback after the talk and i'm an orgy . ch on Twitter I'm old tion get help and the company website is speech don't code achieve the slash unit the so my talk is called what and why you might wanna go casings and but thinking about that uh it's maybe another great talk titled because before I can explain you why you should go easy that should probably explain what I mean when I say a sink and so who here in the room that understands what they're things they understand what what I mean when I say sink or was used in OK most people so you can probably you can go to another talk not uh OK so yeah a sink is short for asynchronous and more specifically when I say a sink I mean asynchronous O and especially in this talk I will be focusing on asynchronous networking and so reading and writing to a socket doing http requests responding to is to be regressed doing database queries that kind of stuff and it's the opposite of synchronous or blocking calls a embroidered this lead to
illustrate sinking versus a that synchronous versus asynchronous usages give some examples of libraries on either side so the probably most famous synchronous uh library and the python world right now is requests which is made at http client library that probably most of you are familiar with its synchronous as is gender which is about primary fast also with primitive all of these are of blocking and synchronous and that posed as libraries classical libraries and libraries and public WS when actually most libraries you will find type I will usually by default in a synchronous and blocking mode of operation so what are some asynchronous libraries that we're at there's twisted which has been around since the polyps the bronze speech and it's been getting around forever and it's a very very good library if you wanted to an asynchronous networking in Python and they have support for every protocol under the sun and there's also tornado which use as similar to twisted then any synchronous networking library but more focused on each servers and HTTP clients and since Python 3 . 4 we have the same title uh think of a I 0 and a standard library which allows you to asynchronous networking by of the standard library and there's also something which is a flask light and there was a talk about that yesterday as well as with the web server uh with framework but it's asynchronous so the Korea which is now alternative to a single the I O G which is an extremely fast will specify and airBaltic or if you talk to any AWS services and since I'm giving a talk I might as well just shamelessly plug some stuff that I wrote and there's a thing called a kinase which is asynchronous Apple push-notification service client library and the senate which is an asynchronous but private client but if you're not familiar with but private that's you might be familiar with so many and so it's like the but a and in asynchronous I O there's a couple of fundamental core concept and at the very very core there's this thing called the mental the event loop these that's thing that will run all you other code and it was scheduled all your functions to running the correct kind ideally and will handle that I that is coming in and that needs to go out and making sure that your stuff gets called uh whenever it's possible and the other fundamental block of asynchronous I O quote that the space is a thing called coroutines previously when we did asynchronous a code in Python we did just call back how uh so you write callbacks for everything at these days we can use a whole routines and continues the function which allows you to do cooperative multitasking and that's where the name comes from its corporate its and the coroutine routine is basically a Python function but 1 that is aware of asynchronous networking so in general amazing O and what we try to do is to have an ideal so anything that reads and writes to suffix of the network to be non-blocking and however logic and by logic I basically mean if statements in Python and still looking so yeah
the is sure crash course of what they think is or what I mean when I say using like like you might like you might want always think the answer is actually simple and I will just give it to you it's
money and you can see a lot of money by moving from synchronous uh network servers or synchronous clients at asynchronous servers or clients and but again the title why
you might wanna go easy is probably a really that title and a better title might be why does going sink save you money so very common
mantra you hear people complain about Python is that Python is slow and then read the 1 of the reasons why people see that especially when they compare it to something like no JS or gold mine it's for is that in an hour default world of synchronous Web services just Django a single process or threat can only ever handled 1 request at a time how so that's obviously not all not enough we cannot just half hour at hand 1 request at a time so what we don't generally did in the past or even in the present these which respond up more threats respond more processes we spun up more servers add to handle more than 1 request at a time and doing this we waste a lot of resources primarily CPU but also a lot of memory because all these extra processes and yet the need extra memory overhead to run and the thing to realize the and where you might wanna go amazing is that most of the time you spend on your right of web server or OK if you write a web crawler or something like that is that most of the time you're not actually spending and doing Python code direction just waiting for somebody else to give you data that is my your database this might be a 3rd party services might be the website trying to download might declined trying to upload a gift of a cat the so actually surprisingly little time is wasted in your Python code your template render engine is usually not the bottleneck or where you waste most of your time so yeah this whole
mantra of Python is slower again it's probably not an entirely true or at least not very honest and I would rather say Python is inefficient but it's also not true because pattern is an efficient and synchronous Python it's if you're writing synchronous Web service thinkers Buckland synchronous database clients you're wasting a lot of time and just waiting for nothing so let's have the simplest Python come to our rescue and I will give an
example here an I been talking a lot about web servers another talk continue to talk a lot about web servers because I believe everybody understands roughly how the web works and that almost this also applies to databases of kinds servers what what what not the in this example of the the uh I will implement a little uh handler that handles a POST request if you read the post state that the client sends us and save it to a database and then returned some identifier as chastened to decline so let's implement this in a synchronous web framework uh this is that I hope you can sort of read it and I don't know if it's big enough and but this is not a real but Americans just how you might do it so you have a handler it takes a request returns a response when a 1st run aligned reads the pose data from the client 2nd line it's a to the database and the last line just returns the response so what happens if we have to request coming in and they're on the right side and comments and so yeah the 1st request handling it reads the post data it saves it to the database the 2nd request is still waiting on the top cannot do anything the 1st request is starting to write the response 2nd request is still waiting patiently at the beginning and yet the 1st request is finally done and the 2nd request is start being handled and it will also go 1 step by step through this then this is a very simple example but I hope that you can sort of understand where the fundamental problem of synchronous servers lights so let's rewrite this amazing with handler in an asynchronous fashion and it might look something like this and the the key nite among you will have noticed this crazy thing that I so that I can no longer for define function which is death handler I give it is crazy argument a keyword based and also we don't return not a response anymore but at the same keyword uh is what defines the called routine functioning Python at prior to Python 3 . 5 there was a decorated to do that and now we have this nice keyword to make it easier and an important thing to understand about asynch keyword is that using keyword doesn't make your function asynchronous the keyword allows function to potentially be asynchronous because if we look at the 2nd line and you see this again this tracing the key word that you might not yet be familiar with way that await keyword is actually what nature makes a function asynchronous and in the previous example when we did the thinkers example when we got the request which is accessed the post on an ad given a request object but here we call these get closely the function and the reason for that is that in a synchronous webserver traditionally if a request comes we read the whole request packet into a Python object and handed over to a request and the but an HDP request is usually compromise of a request line which indicates the HDP method and the path followed by a bunch of headers and then potentially a request the request but he might be very large they might be a bit you might again be a gift of a dog or a cat and so in the asynchronous world usually we only need to request line and the headers so at that point the request handler can already do a lot of time and checking it can check if the path we're trying to access even exists if not not just stop the request and that stuff and not waste any time reading all the data from the client or it might check cookies or authorization headers to see if the client is even allowed to do what they wanted to but the change but what that means is that now we can consider accessing the pose the the we have to await the post state that and this is where the using the magic comes in because while we await the state that the right Our events our asynchronous favorite can go handle all the other requesting call the Python code and when we eventually get all the data from the client it will return to our function and assigned to data and we'll continue and then in the 2nd line and we do basically the same thing when we write to the database the right to the database and then we have to wait for the database to respond with an yes I safety site the database and again because of the elite keyword and while this is why we wait for the database to respond or finish responding we can go handle a request and 2 other things and lastly in rather than just returning a response object but usually happens in sink is you create a response object somehow from a request advert HDP status in the headers and then you write to it you can write it multiple times and he eventually finish it know in our simple example we don't really need that we could use return response object because it's so simple and small but but imagine you generating a CSV file on the fly and you might query a database of multiple times to fill the data and in that case you could do 1 query and then right at the this year's he invited directed to the client and asynchronously keep filling data from your data store into this year's me and then finish it and this way the response will be a lot more responsive on the client side so again let's see what happens if we have 2 requests coming in the 1st request will start getting into the read both data and then while it's waiting actually the 2nd request will also start reading posting and then they will maybe 2nd request was faster because it a better Internet connection or the 1st request operated a huge amount of data the 2nd request is a tiny little bit so they can actually get out of sync and but they can just step through that kind of in parallel or um together and be done with so why does this work again works despite still having only 1 process we only have 1 thread but while 1 request is waiting for data to right before they get to be written and returned to the other requests can be handled by our system
the and if that whole gold was confusing let's just gave an analogy of a restaurant matching you open a restaurant here on the beach in and the customer comes in and they want a 5 course meal you say no problem sources found and you start serving them the meal and now the 2nd customer comes and they want a coffee and you say no sir or no man you have to wait till this other person finishes the 5 course meal before we will give you coffee but this is a problem because now we have like all these people waiting outside a restaurant at the server so obviously what you gonna do is to open more restaurants so you can still serve want customer the time that's what we do in Django and flask and and alternative and I know this is crazy but an alternative would be to more than 1 table if the customer comes in you see them at the table and then you have your event or you're waiters go to them and ask them what they want to send your database or at your kitchen I'm sorry and they will serve them data at food and then that way you can you kitchen still doesn't have to be had able to handle everything at once but you customers can gradually get their stuff that earlier so let's bring all these theory
uh more to reality and a little bit of a story kind of where I work and my day-to-day job is building a single sign-on and access control uh systems where people can log in to various services so we need to handle a lot of walking requests primarily on Monday morning when everybody comes to the office and starts a computer so we've been using tornado for a while but we have not really been using it correctly for various reasons and he wasn't really a sink so what we did is we converted our main had wasting and we're still using Python 2 . 7 at this point we're still using the normal Tornado system and we're actually making heavy use of threats here at this stage because we have a lot of legacy code that realize the legacy API is etc. that would be hard to choose to operate to a sink in 1 go but so we just basically move all our final out into uh this up as something called a thread executed and looked at it and actually just doing this we were able to reduce our number of service we need on a Monday morning by 25 % there's something like say 25 per cent that's nothing and it's not even an order of magnitude who cares but in our case it was actually several thousands of dollars a month and the very very interesting thing here is that our business logic to thing we actually care about the part of our application of dust interesting things or what we think is interesting things stay the same all that he had the changes those little bits of coal debt and do I now this is because we already reducing tornado hearing on chain but it'll be harder but still the interesting thing is that these days if you're doing asynchronous Python code you're cold will look more or less the same except every time you use I you do I owe you might have in weights statement or something like that so let's get back to my
talk that why you might wanna go AC and maybe a better question then that would be widened might might wanna go racing now because the has been around in Python for our I've been doing twisted scenes basically when I started learning Python I double with it a little bit and dating doing a single since forever so why not and the
very big reason for that is basically Python 3 . 6 1 1 and facing there was evidence and 11 free . 4 but you still needed to use decorators to call the final coroutines you had to use yield new from and all these kinds of things that just didn't really feel right and 3 point 5 at the sink and await keywords which meant you can now make your cold look beautiful and 3 . 6 at a whole set on a whole slate of new great tools such as the sink iterators facing generators and he's have anything for you can you know these things come list and dictionary comprehensions and at the scene context matters so just like synchronous called where you would use of its statement for they if I can use of this statement for the current session or a database section or something like that but in an asynchronous context and because of this whole thing with Python 3 4 3 5 3 6 doing all these amazing stuff in the standard library what actually happened in the wider ecosystem is we had a little asynchronous revolution in Python all these crazy project started popping up left right and center and making days in environment in Python a lot stronger at the approach a called the supporter called UV loop which uses the same event loop that um no chess is built on which is very very efficient and he brings that to Python so it's compatible with Eysenck I also you're using a single you just switch it out and according to their benchmarks it's a lot faster and there's this thing called the I O K G which I mentioned before which is is post as client library which again according to their benchmarks is faster than for example the goal and as kind which I don't know how they do it but apparently they do it and it's amazing and they're sending which the some of you might have seen the talk on yesterday which is a fast like web framework and that is also incredibly fast and there's all these other BIO libraries out there and these days and that we'd like to build trade good stuff and the greatest probably the greatest thing about this and I O being in the standard libraries that we now have a common way to do a sinking Python which means even tho we are using tornado thanks to its and I I'm now able to use the sink I O libraries I know what I'm able to use to with alive as I'm able to use all these libraries together and thanks to the standard standardization that think I gave us so the continue this little success story we had made our locking handlers from before and what I did a few months ago is I upgraded our at from 2 . 7 to 3 . 6 . 1 the main thing I did for that is I replaced the tornado event loop with a zinc I went it's still tornado it's submissive use the framework which is used different I was able to remove about 20 dependencies because there were no longer required 3 . 6 because they fix things that fixing 3 backward itself to to and I don't have to rewrite really to dependencies we had an hour a kinase kind library to talk to Apple push-notification services and some Google API snow the lake as run Birkin 3 . 6 and but the upgrade path wasn't clear and obviously the Google client libraries synchronous so it wasn't really interesting for us so I had to rewrite it and we were able because of this which releasing loop we were able to use some of the basic idea libraries that are out there and use better and get rid of some of the fretting we need use and use actual Inc the result at the time speedup of our request the and I'm gonna be completely honest I'm not entirely sure why it is 3 times faster but I'll take it with and so yeah to at uh by manipulating the 1 you wanna go easy to save money primarily and you wanted to go always think now it is because it's ready immature in Python and it's really nice and also to use and please use Python 3 point 6 it's not as great for a sink is great for basically everything out there and yet we that I thank you very much
for your attention and if you have any questions please do read and or because would fj they thanks for your thoughts very interesting and in the current you to long-term support versions if you for so I mean for a the Python versus 3 . 5 which you run that for which you install 3 6 anywhere on the I was assuming you're using them then I will result 46 that's that's what I be we don't use the 1 2 we use a different we use stop but we use a different base image and I just install python frequent 6 because it yet you can definitely is 3 . 5 to do a stinky cheese you're not you're like as a developer you will be a lot happier 3 6 of course with the whole rule on the Mac get 3 6 is less than the mean firms were similar yes the yeah you don't think for the talk it is very interesting of uh 1 question my motive for wanting myself in developing this encompassing the item is the generally asynchronic from system support is not really good so you have to Gold who work-arounds to make the advances thinking is encapsulated like a dedicated to some principles for like a file system storage and retrieval and stuff like that so how do you manage their the yes so n it's called a sink I but uh 1 big part of the ideal spectrum which is files is not actually included usually and that is because from what I understand I know it's expert on that part but I as far as I understand 1st and the Linux kernel doesn't really bored Linux file systems don't really do a sink I know at all so there you have to find ground anyway and yet the general grants suggestion seems to be using Freddie pools what I found is that in because we're on a web server usually during a request we never have to hit the file system we need to hit the file system when we start to get a lot to load certificates low load settings so low that template that's there so we choose do that in the stock up and can I take a little bit of a pen and performances on start-up have to sort of be a little bit slower but then try to avoid to hit the fastest and if you need the file system threats are basically the only the best options you have here been but the thank you it isn't always seem to be very easy to tell in advance what the gains for other gains of results games you might make fine putting in the efforts to go asynchronous in fact that the you would even sure why you're getting some of the games you did what strategy would you used to decide whether or not to put the effort in so yeah and so would this be the the the important thing to realize here is that these include the facing will not necessarily major at false we will make is making more efficient so a single request might be handled slower than if you were in a synchronous world because it has to share some of its time and resources with other request but if you have a hundred requests in general those 100 requests will be finished in an aggregate faster than if you they did it in a synchronous world so yeah that that's 1 of the reason why we were surprised to see such a speed up because we were expecting to just see a lot less CPU usage in lot less memory usage when reading that expected to translate directly into a speedy and that to your question them about how you should choose if you wanna do think or not I would see always go because these days it's not that expensive uh but not that taxing on on your mental and thing if you're doing it but for your programming because it's uh getting a lot more convenient and easy to write and read the sink cold so I would actually say you and you probably always multiple facing simply because after all the benefits you get from it and the only reason why you might not want to go sink is if you were strongly bound to an existing framework Recall basis a channel flask whatever or if there's really no way to talk to the 3rd party services that you need as in an asynchronous fashion and a the hi I was just wondering however the only able to shoot yourself in the foot if you were for instance would it the weights a function that as article competitions instead of doing at all it's sorry if you're doing if you're reading a function doesn't do I all but that doesn't I was wondering why I'm still in yes yes so if you call up if you call if few that week the function that is not a routine it will just give you an error if you wait a function that is the core routine but is actually doing computer like heavy blocking competition of stuff that is really that bad because it will block your whole event loop and there's the environment variable you can set common for development called Python easy bark it is set to 1 it will actually in the consul warn you if a single call to a coroutine takes too long and so yeah if you bring computationally heavy stuff we still have to use the same tools and strategies that you would always using pattern which is eager to send it to a different process or different threat so you don't block you all applications yeah and and it thank any more questions the and you could do you need to thank
Web Site
Statisches RAM
t-Test
Optimierung
Grundraum
Rückkopplung
Web Site
Multiplikation
Browser
Datenbank
Sprachsynthese
Mathematische Logik
Framework <Informatik>
Synchronisierung
Code
Gerichteter Graph
Raum-Zeit
Computeranimation
Loop
Benutzerbeteiligung
Client
Einheit <Mathematik>
Datennetz
Datentyp
Koroutine
Programmbibliothek
Ereignishorizont
Default
Hilfesystem
Koroutine
Nichtlinearer Operator
ATM
Fundamentalsatz der Algebra
Lineares Funktional
Befehl <Informatik>
Protokoll <Datenverarbeitungssystem>
Datennetz
sinc-Funktion
Klassische Physik
Systemaufruf
Binder <Informatik>
Multitasking
Ereignishorizont
Dienst <Informatik>
Funktion <Mathematik>
Twitter <Softwareplattform>
Loop
Verschlingung
Geschlecht <Mathematik>
Server
Speicherabzug
Socket
Programmbibliothek
Standardabweichung
Client
Server
Systemzusammenbruch
Synchronisierung
Computeranimation
Server
Prozess <Physik>
Zentraleinheit
Gerichteter Graph
Code
Synchronisierung
Computeranimation
Richtung
Data Mining
Benutzerbeteiligung
Web Services
Code
Volumenvisualisierung
Drei
Default
Spider <Programm>
Datenhaltung
Template
Einfache Genauigkeit
Dienst <Informatik>
Rechter Winkel
Festspeicher
Server
Overhead <Kommunikationstechnik>
Computerunterstützte Übersetzung
Zentraleinheit
Sichtbarkeitsverfahren
Bit
Web Site
Punkt
Prozess <Physik>
Natürliche Zahl
Mathematisierung
Schreiben <Datenverarbeitung>
Gerichteter Graph
Code
Synchronisierung
Framework <Informatik>
Computeranimation
Datenhaltung
Internetworking
Benutzerbeteiligung
Client
Multiplikation
Lesezeichen <Internet>
Web Services
Mustersprache
Endogene Variable
Analytische Fortsetzung
E-Mail
Gerade
Autorisierung
Einfach zusammenhängender Raum
Lineares Funktional
Parametersystem
Fundamentalsatz der Algebra
Datenhaltung
Systemaufruf
Abfrage
Physikalisches System
Elektronische Publikation
Ereignishorizont
Endogene Variable
Objekt <Kategorie>
Rechter Winkel
Cookie <Internet>
Server
Identifizierbarkeit
Wort <Informatik>
Computerunterstützte Übersetzung
Lesen <Datenverarbeitung>
Aggregatzustand
Server
Bit
Punkt
Gewicht <Mathematik>
Mathematisierung
Zahlenbereich
Kartesische Koordinaten
Computer
Bildschirmfenster
Mathematische Logik
Gerichteter Graph
Physikalische Theorie
Code
Computeranimation
Prozess <Informatik>
Code
Äußere Algebra eines Moduls
Thread
Ereignishorizont
Analogieschluss
Metropolitan area network
Befehl <Informatik>
Quellcode
Physikalisches System
Ereignishorizont
Office-Paket
Dienst <Informatik>
Verkettung <Informatik>
Thread
Loop
Analog-Digital-Umsetzer
Mereologie
Ablöseblase
Server
Gamecontroller
Single Sign-On
Größenordnung
Normalvektor
Tabelle <Informatik>
Resultante
Bit
Punkt
Iteration
Gerichteter Graph
Framework <Informatik>
Synchronisierung
Computeranimation
Demoszene <Programmierung>
Loop
Benutzerbeteiligung
Client
Rotationsfläche
Koroutine
Programmbibliothek
CMM <Software Engineering>
Generator <Informatik>
Benchmark
Befehl <Informatik>
Datenhaltung
Gebäude <Mathematik>
Güte der Anpassung
Rotationsfläche
Mailing-Liste
Kontextbezogenes System
Ereignishorizont
Data Dictionary
Generator <Informatik>
Computerschach
Dienst <Informatik>
Menge
Loop
Tablet PC
Garbentheorie
Projektive Ebene
Programmierumgebung
Standardabweichung
Information Retrieval
Resultante
Subtraktion
Bit
Gewicht <Mathematik>
Prozess <Physik>
Versionsverwaltung
Zentraleinheit
Punktspektrum
Gerichteter Graph
Framework <Informatik>
Kernel <Informatik>
Loop
Benutzerbeteiligung
Spieltheorie
Koroutine
Mustersprache
Dateiverwaltung
Vorlesung/Konferenz
Speicher <Informatik>
Optimierung
Softwareentwickler
Drei
Bildgebendes Verfahren
Expertensystem
Lineares Funktional
Digitales Zertifikat
Systemaufruf
Schlussregel
Physikalisches System
p-Block
Elektronische Publikation
Ereignishorizont
Konfiguration <Informatik>
Dienst <Informatik>
Menge
Last
Festspeicher
Mereologie
Basisvektor
Strategisches Spiel
Server
Programmierumgebung
Fehlermeldung
Instantiierung

Metadaten

Formale Metadaten

Titel Why you might want to go async
Serientitel EuroPython 2017
Autor Obrist, Jonas
Lizenz CC-Namensnennung - keine kommerzielle Nutzung - Weitergabe unter gleichen Bedingungen 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen und nicht-kommerziellen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen und das Werk bzw. diesen Inhalt auch in veränderter Form nur unter den Bedingungen dieser Lizenz weitergeben
DOI 10.5446/33726
Herausgeber EuroPython
Erscheinungsjahr 2017
Sprache Englisch

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Why you might want to go async [EuroPython 2017 - Talk - 2017-07-11 - Arengo] [Rimini, Italy] Asynchronous programming is becoming a hot topic in the Python community, especially with the rise of popularity of Python 3 and the new asyncio library in its standard library. However, it can still be a confusing and mysterious concept. In this talk, I will explain what async programming is, how it works and how it could benefit you. My goal of this talk is that at the end of it, you have an idea about what we mean when we say asynchronous programming in Python and you know how and when to use it. In the first part of the talk, I will dive into what asynchronous programming is and what it isn't. Explaining the difference between IO bound code and CPU bound code. Then I will explain how this actually works in Python, explaining the idea of an event loop, coroutines and cooperative multitasking. Finally, I will talk about why this may be beneficial, what kind of applications can really benefit from this and provide some examples from my experience

Ähnliche Filme

Loading...
Feedback