Bestand wählen

Writing Redis in Python with asyncio

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Erkannte Entitäten
I'm happy to introduce James so we need is a software engineer writing that Amazon Web Services and to press and the dog the tidying writing that is in place and with a single male given and these high IRI 1 welcome things are being used as as mentioned my name is james irony and today we talk about writing readers in my with a single a little bit of background the reason I started this project in the 1st place was really just want to learn more about a single I heard a lot about it but the role of talking about it I have over the dog spread and really really understand how could use this to write something a little more realistic and at the time I was familiar with the red is and I thought you know right it has some features that looks like it would be really good fit for a single so I wanted to see how far I could take the idea of them so I started exploring writing readers in Python with a single there is no I share this today because I think there's some useful things that everyone here can take away from and the wanna show how you can structure of a larger network and server applications that wasn't very obvious to me from reading the network single node documentation and then I wanna show you a couple of patterns that happened in red is that I think applied in general to and various types of network servers so the basic request-response structure how that looks like and also a couple of other ones that are interesting so publish-subscribe and blocking cues so if you're familiar with red as this would be the ideal pop in india pop walking up and walking right up and by the way if you're not familiar to readers that's OK immunity explaining all the specific features were going to be implementing so sorry about that but but I think that these patterns applied to see chat server into any kind of your own task you job queue I think in general this would be generally applicable Fourier started 0 4 dive into its only give a little bit of a disclaimer I'm not in a single I experts from as I mentioned I want to learn more about it it's a little bit about me and I work at AWS inside primarily work on these libraries apparently were gonna redness central portion of the and the point of this so is that I am more my area of expertise is in Python libraries and supporting multiple Python version that kind of thing so writing in a sink I network server that only 1 of the latest version of Python is not something that I would consider myself an expert in some just a beginner trying to share what I have learned from so without warning as much an attraction for this which is probably 1 of the experts in a single maintains of 1 has to do is just a chance might end up you know something like this or something
crazy like this so this is a warning
I get to the 1 Introduction to write as it is a datastructure services and network server you run it on a machine you connect to the sockets and send requested user responses and the 1 thing that's kind of interesting values can be more than strings
so you have your basic a series can set you know through bar and get a value back and I can also get a value and then it'll give you that value back but in addition to that I can also have list so so In this case I can say are push which is the right question onto the food lists the value of a so this is like food out hand and a and I found that you have heard 3 elements a b and c and on the right hand side I can say pop which is left up from through has been returned me the 1st element and I can do l range from through 0 2 2 which is basically a slice the give me the elements and from 0 to 2 in this case is being the because we popped a previously so that's the 1st thing I want shoot for and when I look
at how you basic request response at the end of this 1st section on be able to send a GET request interpretive figure our house is going to work and then send a response back so I didn't think what most people would you start
the documentation and if you haven't seen indoctrination this is the case in Paradox we start at Section 18 . 5 . 4 . 3 . 2 the obvious place to start which is the TCP echoes server protocol and and so I changed 1 line
here but this is essentially stripped from the docks and by the way I'm showing a lot of coherence among the highlight the specific parts of it being very interesting but it's OK if you don't have to follow a understand all this up public the slides online so you can look at it and more detail later but 3 parts to this setting of that person is reduced credit event we were going to then call serving give us the right server protocol so this is a class we're going to write and this is going to give you all the logic for handling the particle passing through you how to call into a database stuff and and then this will set up the listening socket connection we need do is call run forever this will wait for network I O and then call the appropriate things into a protocol molecule will look at that a 2nd and the last thing we need to do is just clean up so once you want axis we closed on the server and call it up close but on look at this right server protocol and I should mention briefly that there's a link to other ways you can do this you can use the low-level sockets in and a single loop and you can also use streams as usual at a higher level what I found streams while while they were really nice to use the performance for me was it was substantially slower anything particles for me with a nice middle ground between and still having fairly being very expressive but having and pretty good performance shows the performance numbers at the end so here is a particle that
is you have connection made data received and get transport for connection made mandate is available you called data received so I looked at this and I can understand what it was doing and I think that there's some some history there from some of the other frameworks that influenced this but and I still don't really get her particles words and for mean going 1 level
deeper into a single trying understand how this works and really help illustrate for go kind of briefly
into a single code to understand how particle transports work because that's really going to drive a lot of its implementation so 1st thing start with other writers server protocol and in this loop what's going to
happen is whenever connections made on basing their this and that is in the top right this is a sum goes last selector events so it's going to happen if you recall that whenever connections made you call except connection but to uh and what's going to happen here is notice a particle and transported created for every connection to return a client connection incredible article in the transport and this part here with a particle factory that was a register particle class we passed in notice was a class objects not an instance so it's going call a class that's going to return an instance but the main thing here is that there is 1 protocol transport per connection so if I had 3
clients connected I would have 3 transport protocol pairs and then the next thing now is let's see how connection made and data received economy
called so if I look at and this is the selected socket transport still amazing code and when you created is as 1st line
here which is softer particle the connection the connection is the thing that we're going to write you can see how it's going to be called elliptical enough and confusing it's really just live thought schedule this thing to run a loop . add this to your to do list of callbacks but essentially what this is asking us for this method to be called and then the arguments to pass to notice that the last argument itself and the clustering is the selected transports we'll see that again and go back to particle classes the next thing here is that the reader ready method so again another call back and look at the last argument read ready and I
will dive into a reader ideas so code again finishing some highlights here and the main part of this is this protocol that data received again this is the method that we're going to right so it's happening here than ever there's data on a socket will read from it the socket . receive and then called data received so the main thing here is that the
this is called taxonomic coroutines not anything new investor like that we just saw just simple called I think it's scheduled whenever a new connection is made and so if event like
going from left to right if I had for connections as it becomes in a culture to receive want those protocols so soulfully that gives you an idea of how particles and transports and work because fleshing out the basic get some response here
and this is how more realistic data receive might look as the 1st interviews
candidate over the wire in contour to pass wire protocol on simplifying things have been all discussed at the end but retain a pass of the wire protocol and so this would give us a a list where the 1st element is the command set next 1 is a key index with a value velocity that we look at what command provided and then after that will call into the DB layers so as a self that B . debt and self . set and look at how that looks like and then finally after that we're going to take a response after serializer to serialize 2 bytes and then use transport that rights to transport is really just an abstraction over socket that allows you to write data back to the clients whenever we have some isn't required we say self . transport . right and so this is the
basic overview for the 1st part and also notice in connection made I should mention restoring the transporter that are able to write back to the client that's 1 connections and then just to give you some
concrete data this is you have to know this is this is the reader serialization particles with the text-based format just how would actually look like get data over the wider looks like this as stars like the list type 3 elements and you can see there's a set of food bar there and then this is what we return to just to give you an idea of what we're actually sending back of the wire OK
annexing the DB class this over all the logic happens for the Reds commands like that instead of the list manipulation 500 and you can see here it's just essentially an abstraction of a dictionary but the main things here that I found the DB being a separate module so doesn't know anything about a single and that that's really helpful so you can imagine how ready unit tests for this right you create a DVD call sets through bar and then insert new TV . that is going to equal bar so it's a very straightforward to test and here's how some of the
list commands of work and again just I hope we can kind of get just this right for our quest for essentially manipulate list the L range with that it's looking up that and slicing based on the start stop and same thing with all proper popping from the front of list by the way you use their collections of texts we get the constant times top left but I'm just as an example and you can
see a concerted integrate that into the particles here so you realistically probably extract into command hammer class but terms adding a false statements to the state that are pressuring to figure out how to construct the appropriate columns the database layer committed that nice level of that nice separation between these encoding and are logic
OK so this part we cover the basic stuff we know how to get from we know how to respond to again acetylase command you could use to flesh out the other types so sort of set hashes all that kind of thing and you get a lot of the commands implemented this way but only cover to more interesting cases that I think it took me a while to figure out so the first one is publish-subscribe from what
we wanna go after here is that we're going top down through the client that connects and says they wanna subscribe to a channel called you may have another client wants to subscribe and what's going to happen is at some later point of another client comes along and says I'm going to publish on the food channel this message that we want to be able to write every subscribes client for that channel that message managed to do something
a little more concrete here but that seemed example that's um actually using real reason already alive so see here's a minute subscribe to channels and then on the 3rd channel here I'm gonna publish and the main thing what you see is when we published in the bottom thing here is to see the top to get the message and that's what we're shooting for so published 2 messages was the first one you see how both got that message and we do it again and so then they both get the next message so that's ideally what we're trying to implement here so how do we do
this and remember that we had 1 transform particle per connection we need something a little bit different we need to be able to from a given transport protocol pairs somehow communicate to other transports they're interested in right it's them marine take advantage
of this is particle factory so I should just amazing that code word in particle factor instantiating according take advantage of that in a 2nd
but the actual pub sub part is pretty easy were going to create a new class and whenever you call subscribers have a dictionary of the channel name and list transport you give me transport when you want to subscribe to something and then the publishers equally
and straightforward you look up the channel you list transporting iterate for every transporting transport useful transport right arm and hopefully that's that's pretty there's not really a lot of a sink color-coded actually pretty damn for example and when we integrate this same
thing we're going back to reader server protocol so in this
command here were just calling a pub sub objects not describing as passenger self . transport and sending of published so so this part's pretty straightforward the other thing that was can introduces how to actually get all of this stuff wired up together
and the remedies very 1st line we looked at which is for passing a class object here the instance when it changes slightly interparticle factory and there are now past the factory to create server else is going to do is just store reference to class and this store
origin cured arts to pass this is also basically like from tools of partial if you're familiar with that just kind of a direct concept in in the code and so now whenever this gets called were able
to pass in a shared object and so the thing that was useful for me here is that instead of having transport know about other transport and you have accord and all that he just tell the parts of objects in the pub sub managers which ones it needs to call out to and so they can keep logic very simple very easy to test just publish-subscribe lasting look at the
problem most interesting 1 is blocking the spots so
shooting for and I'm gonna subscribe to feel part which is the blocking left pop up on the food key and 0 just the time at which means wait forever so they you to clients notice it and get a response right away but now another client comes along and as an operation on the food lists I'm going to pick that value the bar value and send it to whatever kind been waiting longest so this is essentially like a queuing system right and again just to give
you something more concrete and remain the same thing here create you clients were going to have and both of them block and we're going to publish 2 messages and this time see you 1 kind of 1st message in another client gets a 2nd message so hopefully get shooting for really wanna give it to once we now we need to manage which one's been waiting longest 200 you this
1 was I'm also took you will look at time to figure out wasn't into and it wasn't intuitive to me and that really is the same idea
produce assured objects this is a key blocker object and when we do this we'll start at the
bottom and work their way up to remember that database object which is not supposed to know anything about a single I O so what it is here was the same idea where know there's something in the list we don't have to block you return right away but if there is something that we do have in the list or if there's nothing in less than what we need to do is block instead of having the database object know how to block certain integrating coupling single code just returns and sentinel value that says that and you have to wait that I don't have any data available whoever's calling need to figure out a way and if you go up 1 level of the
stack we can see in the data received the same kind of thing here and we're gonna pass in a database and key blocker loop but here we're gonna call the apart himself as before but if we get something in the case we need to wait this is kind of new power gonna look at and they sink in a way here so called key blocker this new object away key going to get this this thing back political routine and is there don't create task discouraging to remember earlier I showed you we how the data received the call back in 1 of the important things there is that you cannot block that the block that method you blocking tired friend everything stops so the best we can do for you wanna block for something is creating new tasks differ from a threat to something Critique reduce thread conceptually and asking them to run it and and that engine block so that's essentially what we're doing here recruiting encouraging and asking the event to run that it's with OK and then that now we have the corresponding you know blocking part we need the push
carts whenever data comes into the push mentality blocker about it and that's this part here were written creating task with data has announced how the
key blocker looks like and so there's a couple of new things here there's new sink in the new itself will go over how this works and so the
1st part is the asynch deaf wait for and this is new to Python 3 5 is occurring in need current gene energy the important part here is that using a single duchies differ from familiar with a Q . Q and that's used for multithreaded and in multi-threaded scenario can the same idea and essentially what we're gonna do is blocks value equals awaits q . and what this is going to do discouraging is this going to sit here and wait until there's actually data available and because the using of a Q which gives us the 5 of semantics it will unlock the 1 who's been waiting long right 1st in 1st out and so I want to get a value we can then be the transporter right for that single and only that single transport and that and then we
have a corresponding for data for key we don't technically need to use that there's a q-dots no weight which wanted to show you can also do that
OK and I found a facing can wait while conceptually just but like the way where you would wanna block normally I wanted to know what kind of how that worked so here's a very high level of an overview of how we think it will work and so what's going to happen is we did the coliseum right for our task here we freaky happening in the event of given comes along and says are going call this method and then remember recalled you don't get and 1 of the things that I I found I realizes that whenever there is no weight some would keep buried in the guts of a single that there is a yield somewhere is the only way to stop Python code from executing and essentially say the states you can resume it with a coating that and right so there is the yield somewhere deep in the deep in a single and at this point we go all the way back up thanks to the magic of yield from and we have this object future and the only matter this can like that what it's called but what happens at this point is this is essentially frozen so the development knows about this call stack and frozen and then something else comes along and it actually is available for the king and the same data for which is indeed the cued up but an acute output is then going to have a value associated with it and again kind of glossing over the details here to to simplify things but what's sensor can happen is this is going to unlock the future is initiated the Stooges done which will then is scheduled to be run in the event loop than this value here goes over to the yield and resumes and then I get that value back from it you don't get sort of very high level you can kind of see how
that would work with this a sinking awake here that's essentially what's happening here OK so but that is basically how you do blocking the spot using for appeal pop and are part the analyst and there are
a few additional considerations are things that I didn't really have time to cover and then actually changed a lot of how the internals work so 1st thing that the real person is more complicated so I made a big assumption that we're getting all of the data as a single request and realistically you would get partially you would feed the person will tell you when and how to complete and uncomment run and you could potentially more than 1 command of feeding pipeline that kind of thing a pub can handle clients disconnecting right so there is also another method on the the particles that we did look at connection lost and that's how you can handle disconnecting and and there's also like some advanced pattern matching that kind of stuff and then the last thing is that blocking Hughes you with multiple cues like it's deal part you know food bar and then when data is available on either of those who will unblock and server that I can use in a single adult you wish to go a a little more low-level and use the same kind of features OK but lasting
performance I was curious so there's a red is benchmark program you can run the kind of writers and these are the parameters it is it is the basic set and get and on my laptop when a server got about 82 thousand requests per 2nd for pirated server I got 24 thousand initially but I was wasn't there and you start with you argument you the loop so I thought it was tried out see how well it does and just let you we would no changes brought it up to 38 thousand requests per 2nd so that was really cool and where you know a little more than 2 times slow and very unoptimized code of medical father was missing my person code which node can be optimized quite a bit inefficient 100 personnel but about go through pretty close
and just a summary of what I covered here so far but the transport of particles of the showed you a little bit more in depth how they work how they appear to a single connection and then we looked at the request response and then some other patterns for how we can share state or how you can communicate state across the various transport so looking for publish-subscribe non-blocking q like behavior so I'm gonna put to the slides online i don't know where they are William yet and I don't know but the code online as well as have links guess for now and mobile Tweet amount eventually and so far the best place for more information but have thank you things were much chance for this talk we take a few questions the Rachel why did you write you'll all those of you living near Greece's Jean-Paul there there's for example the high rate is called the it is the seat of faces the other of the writers an the question mostly just to see to see what the overhead beaches can learn more about that in most this is a project to learn more about the hardest work had implemented but I think if I was actually going for performance I would be the next step is you try to clean up the person myself or to just use the the higher which is a celebrity the parsing and forming a thing I would get pretty close performance to to the real so yeah that's something I I wanna look into In some more questions 1 of the things 5 we're talking about the you'd better do made that that's where food rats with persistence or not 0 you so you're asking about if we process the data to describe the as yet so there's not something about looked at and I don't actually know the best way to do it with the rest is just as the 2nd writes in the background and that would be my 1st attempted to do some good to be had to do something similar charges the fork exec and figure out the serialization diary the serialization sea and how far the how well that works with a single and but I haven't actually tried it OK thanks OK 1 more 1 last question this of and what do you think about these interaural did you like it the experience or what's your conclusion of so I think 1 of the the biggest problem for me was just it was hard to figure out how to do things and I think you could not come from the background of writing a lot of missing code and the docks and I think they're just mentioned earlier you know that the docks and a little bit of work and it was not I can understand how building blocks work and how coroutine worked and has an elaborate and we get of that things together and I think just having more examples and having more documentation about that would really help as far as the intervals I found a little confusing if the people familiar with is and how you have features and tasks which really should be called like protein driver and how the scandals things and get features that called and where that kind words from a little hard to understand and I've been looking at some of the other ones the other I think premise that security was 1 single lots of the for me to understand them but I think for me the biggest problem is just having more examples of how these things and then space work so thank you very much for it all began meant which
Gewichtete Summe
Program Slicing
Schreiben <Datenverarbeitung>
Gerichteter Graph
Formale Semantik
Faktor <Algebra>
Protokoll <Datenverarbeitungssystem>
Prinzip der gleichmäßigen Beschränktheit
Schreiben <Datenverarbeitung>
Befehl <Informatik>
Mobiles Internet
Gebäude <Mathematik>
Dienst <Informatik>
UDP <Protokoll>
Rechter Winkel
Große Vereinheitlichung
Klasse <Mathematik>
Content <Internet>
Mathematische Logik
Virtuelle Maschine
Spannweite <Stochastik>
Modul <Datentyp>
Endogene Variable
Cluster <Rechnernetz>
Tabelle <Informatik>
Protokoll <Datenverarbeitungssystem>
Konvexe Hülle
Anwendungsspezifischer Prozessor
Binder <Informatik>
Data Dictionary
Komplex <Algebra>
Wort <Informatik>
Faktor <Algebra>
Boolesche Algebra
Streaming <Kommunikationstechnik>
Element <Mathematik>
Snake <Bildverarbeitung>
Element <Mathematik>
Prozess <Informatik>
Figurierte Zahl
Funktion <Mathematik>
Folge <Mathematik>
Nichtlinearer Operator
Prozess <Informatik>
Ideal <Mathematik>
Spannweite <Stochastik>
Twitter <Softwareplattform>
Automatische Indexierung
Projektive Ebene
Software Engineering
Gewicht <Mathematik>
Keller <Informatik>
Framework <Informatik>
Physikalisches System
Syntaktische Analyse
Speicher <Informatik>
Leistung <Physik>
Einfach zusammenhängender Raum
Einfache Genauigkeit
Physikalisches System
Endogene Variable
Objekt <Kategorie>
Serielle Schnittstelle


Formale Metadaten

Titel Writing Redis in Python with asyncio
Serientitel EuroPython 2016
Teil 96
Anzahl der Teile 169
Autor Saryerwinnie, James
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/21157
Herausgeber EuroPython
Erscheinungsjahr 2016
Sprache Englisch

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract James Saryerwinnie - Writing Redis in Python with asyncio In this talk, I'll show you how to write redis using asyncio. You'll see how you can create a real world application using asyncio by creating a python port of redis. ----- Python has been adding more and more async features to the language. Starting with asyncio in python 3.4 and including the new async/await keywords in python 3.5, it's difficult to understand how all these pieces fit together. More importantly, it's hard to envision how to use these new language features in a real world application. In this talk we're going to move beyond the basic examples of TCP echo servers and example servers that can add number together. Instead I'll show you a realistic asyncio application. This application is a port of redis, a popular data structure server, written in python using asyncio. In addition to basic topics such as handling simple redis commands (GET, SET, APPEND, etc), we'll look at notifications using pub/sub, how to implement the MONITOR command, and persistence. Come learn how to apply the asyncio library to real world applications.

Ähnliche Filme