Merken

Python and Async programming

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Beta
Erkannte Entitäten
Sprachtranskript
can everyone hear me yeah great uh I think they were supposed to be 1 of the organizers here to announce me but I don't see anyone and it's time I guess of just started yes so when I started looking to sink I I notice that a lot of people thought it was quite complicated to use especially to get started with so again this talk the subtitle of the need to deal with the users and the answer is still a little bit it's not that hard but when you're just getting started there
are quite a few problems that you'll run into an will concurrency is intrinsically complicated so it's not anyone's fault for their lot of new concepts the things you need to learn that are not just standard items and has a very long history all the way from the back porch to 2 . 7 to the new keywords in 3 . 5 % that's change a lot of the things you you have changed a lot so when you try to figure out how to do something that there's not 1 way to do it but there are different ways in different versions and they will change quite a lot and sometimes they have documented that best practice you will find is maybe deprecated now so what that can
be a problem and if you don't know the internal something's will just be we here but and you could use
to those quite quickly and unfortunately the code is quite easy this coding that's what read so you can look at the internal figure those out and there's a lot of incompatible terminology is like coroutines are generators except when they're not generators and not all generators inequalities and you you have the eyes like future that implements the same API but is not you can't really use them interchangeably and will
run into trouble with those things and since it so new there's this bottom-up approach to learning a zinc ion so
you wanna and it's very common that you started understanding 1st that workforce generators coroutines and have to understand all the internals before you actually start writing code think I so
maybe it was try to simplify this and try to go through however the interesting case when how a started writing
things and years I've run into and see how other people can start writing code with the from this and I I work at a web agency and I came to single because sometimes means we have to do are concurrent nature
and I notice that some of my coworkers just started going to other languages mainly goal and say well this is too complicated to do in Python out as writing goal creative services and then we can call the service to to that to solve this problem but also I realize that Python
just added this whole thing to do concurrency that change the way Congress is supposed to be done in Python and we were not really using them so I tried to see what can we do this with a single instead and some of the problems we're trying to solve with things like creating a back-end service for server for real time the with server sentiments or websockets and that would look something like this this is a very simple echo server but it's not very far from something you could actually used to launder WebSocket but we could for
example try to fetch data from several API concurrently which I think is something that is very common especially when you're doing service-oriented architecture you may have tons of API is that you wanna get data
from and if each 1 of the States about 100 ms and you wanna query 5 to 10 you might end up spending a whole 2nd just waiting for I O 1 you could do this concurrently so this quite easy to do with KL would
look something like this you don't have to pay too much attention to this example so look at them in more detail later is you can do some sort of pipeline processing which is very similar to before but instead of just catching the or else you may want to process them afterwards maybe 1 of them in red is maybe you want to offer Jason and all these things you can do a synchronously
and you could copy insight into this began such a point you will also then lock a file and write to the file low you may want to have a complex data structure that you wanna have a memory and you 1 axis but you want every now and then this gets loaded from a store somewhere and develop something like this this is just a sample code but you would work finally the same way and so as you
can see all these examples follow kind of the same pattern and that pattern this they concurrency model that we're going to use it and there are several models that you could use in different languages and many different ones and there normally a combination of different concurrency models and so I think they always basically a formalization of how we should do concurrency now I think there are 2 parts to concurrency models the first one is how we write and understand the code I think this is the most important this is the 1 that we should focus on you can see that as in the eyes if you want to write an API for concurrency you could come up with something like this you could execute something that foregrounds spectacle something background which were sold or scandal execution for things later you can write some functions to do this and this is just like at pseudocode of something we may want to do with what happens concurrency model and if we did that concurrency model that would yield programs that look a certain way now a sinking will give this programs that look India sink away
and then a unified model of how we do concurrency so the other part of concurrency model is when the cold and they can
run traditionally and threatened process you can run a different machines if you use something like MPI or you can run an event of an event that was a big part of what that a model builds upon and I don't I'm not gonna look too much into this part of things this that thinking matters that much I'm going to look into
the other way that is how is the code going look and how we can understand that code is correct and that's what we want from the point of European currencies and then these things might be interchangeable
later so let's start looking 11 2 by consensus questions but before we do that I want to tell you what the scene programming Python is not and that is just 1 thing and is a solution to your problems so if you are writing code that runs a lot of pride and spends a lot of time in the by the interpreter you're still gonna run into the global interval lock and this is not gonna save you from there are some projects like they
colectomy and pi by STM the users suffer transactional memory that are trying to fix this in different ways but you will have to see where they come up with but for now if you're gonna do something that is that the deal is getting in the way then you'll just have to go and rhodamine different processes like we've been doing for years so OK let's get
started let's write some code but but before we do that I wanted to I have discussed this 1 just to get out of the way and we said that things can run in threads and processes and that's fine
we can create a thread and with Fred holds we can then tell to execute a process and it would look something like this but we can also run things and events and this is the 1 that is new people wanna around something in event we 1st need to know when an event of this and anybody work with JavaScript probably knows this and many of you probably do but in case you
don't it looks something like this just a little that will run essentially forever and will pick a task and based on some policy and after that has that has to be executed and that test will execute until it decides to suspend executions and but as you can see there's nothing here that is preempted like with threats the task will execute and eventually that task will have to tell them that it has returned from functions so that can continue running otherwise that you will be blocked right there once upon suspends execution and then you can reschedule the task the size of the task as a result and he wants to do something with the result may want storage
otherwise we just put that task that back in our and selected again when we look back and it will 1 is
an event of type and it looks kind of like this you have a function and you can define a function with be a single word about 3 point 5 and when you define a function that is supposed to run synchronously that it's this keyword tells the function that is supposed to run asynchronously so that function will not execute unless a new courses and then we have the await keywords to tell the loop when it's supposed to suspend executions so that we get an event log and we tell it to run that function until it completes and that actually work and if we want to specify wanna run this thing some threats instead we don't write a synchronous functions and we take a normal blocking function that we want run threads and we have this utility called running next year that would actually allow us to run the function and will return something that is a synchronous and that the knows how to handle so this gives us the flexibility of being able to write our code
write code that will look the same way and then choose whether it's gonna running threats process or an event and have everything control through this event abstraction now
let's start writing some sink where we need to know we need to know what the async and wait syntax which if we want to have a function that is not as simple as that of flux then that's fine but Frederick function and we call that whenever we needed if we want to have a function that runs in the event of and they can suspend execution then instead using keywords and that function will not run by itself inside the function we can use a weight and wait will just it's very similar to a return except when the function comes back he will continue execution from there so we tell the loop that we will suspend execution on until some other function or awaitable has finished in this case it would be suspended executions or that execute were were running the blocking functions and what 1 of the advantages of this is that this way we can specify explicitly when our code will suspend when he was so if we were were to run this with threats the operating system will tell us
when the function can stop all 1 can't so we have to be a lot more careful in terms of synchronization with here the function will continue to run until that function sites by itself that it was not the other building block that we
will meet with this is futures so a future is is basically a wrapper that can you can check it has 3 methods you can check if it's done and you can check if it has a resource for you can add yourself to it so when you have a future and you check if it's done it
normally isn't on less you have added salt and then if you ask for a result doesn't have any will yield an exception otherwise you will you consider result and then you get result and now we know we know this background information so let's try to get a and start running so we have a boom created run and stop instead of things on so to create and we use a single get event loop and that's it now we have a look
and it's not doing anything but we have now if you want to run it you can just call run forever on it and will do successive around forever unless there is an exception inside the loop in which case you would just throw the exception and now that that people want to stop a little and I think a little confusing and you can only be stopped if it's right so if you have a loop when you want to tell it to stop you will have to ask it after you ask it to be marked stop need to ask it to run once otherwise you will not be considered a stuff so if you want to stop the loop that is not
currently running you have to ask it to run forever and you will run once and
then stuff which is confusing is that that things that you have to look at intervals to understand how it works but but most of the times you don't have to do this Is that what you do is something like this you want to run things other instead of managing and manually so let's say we have this function that we want to run something just print a bunch of numbers and weights 1 2nd between each number so you ask them to write to
complete and that's it it's princess we expect it to be just a little boring so and so we would want to do and we've been talking about currency is run something more like this we say well 1 have 2 of those functions run concurrently and if we do that we can use this to ensure future that will just create a future and attach to the little and we write an extra function because we only know so far how to run 1 thing things so OK go look
and then when we execute that that would actually stop the quickly and give us an error
and the reason this happening is because we created these 2 futures but we didn't really nobody's waiting for for them so when the rule of law very runs he runs the growth function and he knows that you will stop when the goal function completes the function creates 1 future doesn't wait for for it creates a 2nd feature of for it and then it completes so that stops running and if you're debugging a single you will get this nice helpful messages so instead what we can do is just a weighted and then we use a weighting would tell the well I wanna run this 2 things by I wanna wait for them don't and go until they complete and if you do that you will work
but if you look at the result you can see that it's actually running 1 one-pass 1st and the 2nd pass after which it's not really concurrent just sequential and you might as well just have written regular functions so Is that what we can do is try to figure this out and go back to the specifications here that we have a single that has completed so we can create this task that we 1 around and then we ask Osindero to give it back to us and for the that in the order they complete and when 1 of them completes we get the future we wait for its results and then this works except
it's really up to its and this doesn't really look very by tiny can it's very complicated for just running a couple of things current so what we have is that is this icon aggregators tools of single where we can do things like a single weights which just will take a lot of quarantines for a list of past and not return until all of them have completed so if we run
something like that were actually run concurrently now and it still looks quite ugly but
we can realize that if you were waiting for a single of the way then that means that I think is the way it's an awaitable hand down means that we can run that directly on little and then it would look
something like this and this looks a
lot more like an API we want work creative and ask and ask them to wait for all of them and executed concurrently and OK I want to make an aside here and remember their internals and how they can be complicated we have also this function for loop to run 1 since running forever or until something completely and we shouldn't bullets and on the score function but it's there so you call it and then you can see
that you can run a loop step by step so whenever you're debugging a single you can just start running step by step and printing stuff and you'll see what's actually happening OK but those
are quite boring functions
there just print stuff so let's try to build something that retrieves some results so I choose a functionalist but now it's a countdown and now it just multiplies the numbers as it goes down and returns the results so we create a list of possible weight on and we assume that the cashier can have some information for us when we get back
thing is what we get it is these qualities and send their quarantine the have already executed so they're not very useful we can't really getting information from 2 can try to go back and say well if I remember futures where something that I could use for knowing the results of things that haven't happened so we create in the future instead of run that and yet now we have
this task in the past it's just that um is just a quarantine wrapped in the future that that the little bit using internally and that's good we can access things from tasks so so we iterate on
over them and that's it will get a result that is just we made this the ugly again so Is that what we can do is go back to our aggregators placed on the Cinco which is they're not aggregators there by the way that's just the name I get and we have this 1 called gathered than those very something very similar to way except you will actually return all of the results and since the loop returns whatever function was running can then we can just
print those results and it does the same thing as before except it just looks a lot better so prepare where we learned so far we have assumed in a way we can create awaitable functions whose execution can be suspended and weighted away can suspend until another way the whole returns we have a look at that we can create we can ask it to run we can ask it to run something run forever and we can stop and we have this functions that allow us to wait for things so that we can run many things concurrently and at the same time get the results afterward features that we wait for all of them so OK that's good with that we can really
start writing some little more realistic code and we can
write something like this this is an example as they said before dispatching a bunch of you wanna get the else and you wanna get em all at the same time were concurrently and then you got the results and you could do
something like that and this works will virtually or else in all the world will be there at the end the only thing is that but maybe we don't care about all of those who were also we can try to do something a little more complex and say well maybe they just care about the 1st URL return so let's write something bad ones to so that's incorrect correct 1st completed and it's very similar to before so we just went back to this task completed the idea that has a time out and we just wait for those of waiting for the proteins to complete and when the first one complete just return that and that's it and this looks like it would be work at so when we execute we get a lot of errors and the reason we would is actually return and you can see the HTML of their there was supposed to be returned but without were getting all this task have been destroyed and that's something close and the thing is with a single so you have to do explicitly clean up your supposed to close your own loops and clean up your own mess so let's start just by closing the loop manually so that he will internally
clean up some stuff doubles but
if we got rid of 1 error but we still have this test spending on the reason those depending is because if
we some yes yes yes if we if we go back to original code and you can see that I We returning after the 1st pass completes its not the 1 that were returning and after the 1st that's complete the other ones are still in the loop and they're supposed to be executed that were never waiting for that no 1 is waiting for the steps so we can do instead this something like this and say well we're gonna execute and do this exact same code except we're going to wait for all the tasks before the function returns and that works that gives us the right resources the only problem is that this space as long as the longest wait and some API so slower
than the others you get some the returning 1 ms on the return 100 ms then you're gonna get the first one that's the result you return and then you're gonna have to wait 100 ms until all the other ones the so instead what you can do yeah
something like this I guess you start working with it you right another functions are all now wanna cancel my past and then within your cleanup you the the 1st from the function that returns 21 break and clean up before closing the loop you cancel all the existing that and the other works it's not nice another by tiny anymore I think so instead with you have this will go back to the API and we use the wait before and weight actually has other parameters like at a time out and return went and we can tell to return when the 1st pass completes an what where return is actually 2 sets of futures 1 for the has having completed and 1 for the past there and so we do something like that for the pending task we ask him to cancel out they completed 1 we just get the 1st future and return that and this will work this principle we want get the result we wanted the only issue is that this example is very well matched to what weight DOS so uh yeah if we want to return the the 1st completed maybe the 1st 2 then this doesn't work anymore so we would probably have to
go back to something like cancel or have to write their own function that deals with this thing but it's good to have this on this several options
that we have yeah we we can either wait and then deal with and the ones for we inches out write a function in the hands of the other ones or we can cancel manually somewhere else but yet so whenever we're working
in a single day the important thing here is how we have to do cleanup and it's not always trivial and we have to close everything whenever we use our sockets readers Exeter's everything has to be closed manually we should expect framework of things for us and we should know where task are so OK on gonna make this a little bit more
fun before we and and now we have this 1st
the 1st complete API and let's say this is a very critical part of our code we normally query hundreds of API
standards on that we just get the first one quick and we might have something else in stack them it's not Python and want to use this so let's just build the server
returns and so for this we just more use the same cold as we used before and then we're gonna try to get the server that search 1st completed so we go documentation and we get that this is basically the easiest server you can build this right from the contagion of slightly modified in right now this echo servers OK so this runs in a single run something and if we want to build our own function into it we can just do something like this means that we're this exactly the same as the aggressor we were on before we just change this code here in the middle that's just calling 1st completed waiting for so um and this works you can run this code and you will Our executed you will run on a server the Internet captured and you and the good thing is that it will right right just from the example and adding function there you will have a work servers that executes and can handle a lot of requests but if we want to do instead of using just playing soccer do HTML we can start looking think they'll libraries and look for example to into the fail http again just saying this is basic a HTTP server just coca function there and we have a completely asynchronous server that is returning while
running such things concurrently so OK is this novel Killea probably we don't
really need to build server for this but it's good that we can and we can do it while running everything within 1 thread in a little while using the
same framework that we will use for all concurrency by so for other use cases may be something we would want to do instead is just random movement of thread and we can do that
too we can have a synchronous code and that that is a normal height and that is identical that will be running and we can put the loop on a separate thread and then assign things to it to run and then get back things get a little more complicated here but this will work and you just have to get Europe make sure you clean things up and make sure you always call for that to long threats and there's a lot not to a single data and they're going to have time to go into their low-level API is this specifically used for synchronization I got quite useful and you can build your own protocols for your servers and there are a lot of people have actually should go check out the internal state if you go to get half and I think you
have the actual code there that is very easy to read and that's basically the whole implementation and out of the external stuff a single 1 that we don't want to name in itself is the possibility of creating alternative event which we can do when can use this framework while using the event study not come with a single and 1 of those things which aren't yet with
users are allowed a lot of things internally but he claims to be very very fast and you look at the benchmark sets of their faster than JSN about as fast as go and you this is something that you can just plug in and you can run any of the code we're inviting
just change this and you you believe instead and you will work with separately so that the flexibility that this gives us is that we can write the same code and understand the cohesin this framework but at the same time we can change the channels that will run in different ways and changed will execute and there are a lot of third-party libraries for pretty
much everything you want so if you wanna circling the single get some of these and start writing some synchronous code thank you thank
you thank you
Bit
Selbst organisierendes System
Baum <Mathematik>
Computeranimation
Metropolitan area network
Subtraktion
Existenzaussage
Versionsverwaltung
Baum <Mathematik>
Computeranimation
Generator <Informatik>
Ungleichung
Koroutine
Codierung
Knoten <Statik>
Figurierte Zahl
Baum <Mathematik>
Code
Computeranimation
Metropolitan area network
Benutzerbeteiligung
Generator <Informatik>
Task
Loop
Natürliche Zahl
Koroutine
Knoten <Statik>
Baum <Mathematik>
Code
Computeranimation
Gammafunktion
Server
Datenparallelität
Singularität <Mathematik>
Formale Sprache
Mathematisierung
Echtzeitsystem
Computeranimation
W3C-Standard
Web Services
Echtzeitsystem
Loop
Front-End <Software>
Front-End <Software>
Server
Ereignishorizont
Baum <Mathematik>
Metropolitan area network
Ablöseblase
Abfrage
Baum <Mathematik>
Computeranimation
Aggregatzustand
Serviceorientierte Architektur
Prozess <Physik>
Punkt
Division
Kartesische Koordinaten
Elektronische Publikation
ROM <Informatik>
Komplex <Algebra>
Quick-Sort
Code
Computeranimation
Metropolitan area network
Code
Festspeicher
Stichprobenumfang
Mini-Disc
Speicher <Informatik>
Datenstruktur
Erlang-Verteilung
Lineares Funktional
Subtraktion
Datenparallelität
Schaltnetz
Formale Sprache
Formale Grammatik
Gerichteter Graph
Code
Computeranimation
Eins
Informationsmodellierung
Code
Mereologie
Mustersprache
Modelltheorie
Optimierung
Baum <Mathematik>
Virtuelle Maschine
Informationsmodellierung
Punkt
Prozess <Physik>
Code
Mereologie
Baum <Mathematik>
Code
Ereignishorizont
Computeranimation
Demoszene <Programmierung>
Interpretierer
Festspeicher
Projektive Ebene
Rastertunnelmikroskop
Optimierung
Baum <Mathematik>
Code
Computeranimation
Prozess <Physik>
Loop
Thread
Ereignishorizont
Chi-Quadrat-Verteilung
Ereignishorizont
Computeranimation
Softwaretest
Task
Resultante
Loop
Speicher <Informatik>
Ereignishorizont
Baum <Mathematik>
Computeranimation
Lineares Funktional
Punkt
Prozess <Physik>
Abstraktionsebene
Softwarewerkzeug
Code
Ereignishorizont
Computeranimation
Metropolitan area network
Loop
Code
Datentyp
Gamecontroller
Wort <Informatik>
Thread
Ablaufverfolgung
Baum <Mathematik>
Metropolitan area network
Loop
Lineares Funktional
Netzbetriebssystem
Gebäude <Mathematik>
Fluss <Mathematik>
p-Block
Term
Baum <Mathematik>
Synchronisierung
Code
Gerichteter Graph
Computeranimation
Resultante
Loop
Loop
Baum <Mathematik>
Wrapper <Programmierung>
Einfache Genauigkeit
Ausnahmebehandlung
Information
Ereignishorizont
Computeranimation
Loop
Ausnahmebehandlung
Baum <Mathematik>
Computeranimation
Lineares Funktional
Datenmanagement
Gewicht <Mathematik>
Zahlenbereich
Computeranimation
Metropolitan area network
Lineares Funktional
Gewicht <Mathematik>
Schlussregel
Gesetz <Physik>
Baum <Mathematik>
Message-Passing
Computeranimation
Gammafunktion
Fehlermeldung
Task
Umwandlungsenthalpie
Resultante
Metropolitan area network
Lineares Funktional
Gewicht <Mathematik>
Strömungsrichtung
Bildschirmsymbol
Ordnung <Mathematik>
Message-Passing
Computeranimation
Gammafunktion
Metropolitan area network
Baum <Mathematik>
Computeranimation
Metropolitan area network
Loop
Lineares Funktional
Hochdruck
Computeranimation
Resultante
Metropolitan area network
Lineares Funktional
Multiplikation
Gewicht <Mathematik>
ATM
Hochdruck
Zahlenbereich
Mailing-Liste
Information
Computeranimation
Gammafunktion
Resultante
Task
Metropolitan area network
Loop
Lineares Funktional
Bit
Große Vereinheitlichung
Computeranimation
Gammafunktion
Resultante
Metropolitan area network
Lineares Funktional
Bus <Informatik>
Hochdruck
Ereignishorizont
Baum <Mathematik>
Code
Computeranimation
Gammafunktion
Task
Resultante
Metropolitan area network
Loop
Anwendungsspezifischer Prozessor
Schreiben <Datenverarbeitung>
Computeranimation
Eins
Fehlermeldung
Task
Softwaretest
Lineares Funktional
Ausnahmebehandlung
Baum <Mathematik>
Message-Passing
Raum-Zeit
Code
Computeranimation
Eins
Fehlermeldung
Resultante
Lineares Funktional
Gewicht <Mathematik>
Computeranimation
Eins
Zustandsdichte
Task
Loop
Metropolitan area network
Menge
Message-Passing
Baum <Mathematik>
Gammafunktion
Metropolitan area network
Lineares Funktional
Baum <Mathematik>
Computeranimation
Eins
Konfiguration <Informatik>
Inklusion <Mathematik>
Bit
Knoten <Statik>
Framework <Informatik>
Socket-Schnittstelle
Computeranimation
Task
Metropolitan area network
Erwartungswert
Task
Loop
Klon <Mathematik>
Socket-Schnittstelle
Baum <Mathematik>
Lineares Funktional
Vervollständigung <Mathematik>
Abfrage
Einfache Genauigkeit
Code
Computeranimation
Internetworking
Metropolitan area network
Rechter Winkel
Mereologie
Server
Programmbibliothek
Baum <Mathematik>
Datenparallelität
Randomisierung
Server
Thread
Baum <Mathematik>
Framework <Informatik>
Computeranimation
Beobachtungsstudie
Loop
Protokoll <Datenverarbeitungssystem>
Server
Thread
Baum <Mathematik>
Code
Ereignishorizont
Framework <Informatik>
Synchronisierung
Computeranimation
Aggregatzustand
Menge
Programmbibliothek
Ereignishorizont
Baum <Mathematik>
Code
Computeranimation
Metropolitan area network
Baum <Mathematik>
Code
Computeranimation

Metadaten

Formale Metadaten

Titel Python and Async programming
Serientitel EuroPython 2016
Teil 06
Anzahl der Teile 169
Autor Lara, Nicolas
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/21200
Herausgeber EuroPython
Erscheinungsjahr 2016
Sprache Englisch

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Nicolas Lara - Python and Async programming How does the experienced python programmer fair when faced with python's "new" way of doing async programming for the first time? This talk details the different ways python provides for attacking the problem of asynchronous programming and focuses on the best practices for the future (as of python 3.4 and 3.5) ----- How does the experienced python programmer fair when faced with python's "new" way of doing async programing in for the first time? Do we all know how and when to use Futures, yield from, asyncio, coroutines, the async and await keywords, eventloops, and others? A lot has changed in recent versions of Python when it comes to async programming, concurrency, and parallelism. We still have very different ways of approaching the problem in each version, but they are finally (as of python 3.4/3.5) converging to a standard. This talk explores, from the perspective of an experienced python programmer with little to no experience in async programming, what the "one obvious way" to do async programming in Python is supposed to be. It does so but analysing examples of different categories of async problems we may want to solve and what the correct way to solve them with the latest versions of Python would be (along with the trade offs of different approaches). The examples include generic CPU-bound problems, IO-bound problems, and "both-bound" problems; along with common tasks as building a simple server, scraping, deferring a web response, and traversing graphs. When useful, I compare the solutions with the approach we would take in languages that have been design for- and are known to be good at async programming like Javascript and Go.

Ähnliche Filme

Loading...