We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Fast Async Code with Cython and AsyncIO

00:00

Formal Metadata

Title
Fast Async Code with Cython and AsyncIO
Title of Series
Part Number
22
Number of Parts
169
Author
License
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
Stefan Behnel - Fast Async Code with Cython and AsyncIO Learn how to use the new async/await language feature to write asynchronous code in Python and [Cython]. See how to benefit from the excellent low-level features that Cython provides to speed up or parallelise your code, interface natively with external C/C++ code, and achieve better responsiveness and lower latency also in mostly I/O bound applications. ----- Python has recently seen a fresh development boost around asynchronous applications, triggered by the addition of the asyncio library and the new async/await language features in Python 3.5, but coming from a world of well established tools like [Twisted] and [Tornado]. The [Cython] compiler, which compiles Python code to C, has accompanied and influenced this development. It provides full language support for async/await under all Python versions starting from 2.6, as well as native interoperability with existing Python code and the new Python coroutines in Python 3.5. Benchmarks show that, while fully compatible, Cython compiled coroutines perform about 2-3x better than the same code executed in Python, but they additionally allow to interface natively with external C/C++ code, release the GIL, do parallel computation, and much more. All of this extends the applicable zone for asynchronous applications dramatically and can lead to better responsiveness and lower latency also for mostly I/O bound applications. This joined talk by an async I/O expert and one of the Cython core developers explains how to write code with async/await in Python and Cython, and shows how to benefit from the excellent low-level features that Cython provides on top of Python.
Multiplication signWebsiteCASE <Informatik>XMLLecture/ConferenceMeeting/Interview
Core dumpSoftwareProjective planeDisk read-and-write headType theoryInductive reasoningProcess (computing)Right anglePay televisionService (economics)CurvatureBit rateE-bookComputer animationLecture/Conference
Bit rateBasis <Mathematik>Product (business)Front and back endsComputer animationLecture/Conference
WebsiteTrail1 (number)Computer animationLecture/ConferenceMeeting/Interview
Default (computer science)TrailSoftwareComputer animation
SoftwareTelecommunicationLibrary (computing)QuicksortLaptopLecture/ConferenceComputer animation
Cartesian coordinate systemFront and back endsDependent and independent variablesWeb serviceMultiplication signProcess (computing)CASE <Informatik>SynchronizationStandard deviationDatabaseTask (computing)WebsiteBefehlsprozessorRight angleScaling (geometry)Universe (mathematics)Single-precision floating-point formatLecture/ConferenceComputer animation
WeightBlock (periodic table)DatabaseMultiplication signTask (computing)Endliche ModelltheorieSynchronizationLecture/ConferenceComputer animation
InternetworkingCartesian coordinate systemRun time (program lifecycle phase)Total S.A.Task (computing)View (database)Ideal (ethics)Event horizonSoftwareMachine codeSocket-SchnittstelleGame controllerBlock (periodic table)Loop (music)Network socketLecture/ConferenceComputer animation
Interface (computing)Social classConcurrency (computer science)Interface (computing)Library (computing)Service (economics)Software frameworkBlock (periodic table)WeightFree variables and bound variablesResultantSoftwareOperator (mathematics)Link (knot theory)CoroutineLecture/ConferenceComputer animation
ProteinMechanism designDeclarative programmingObject-oriented programmingDirected graphCoroutineProcess (computing)QuicksortOperator (mathematics)WebsitePoint (geometry)Web browserElectric generatorResultantFunctional (mathematics)Game controllerLoop (music)Network socketQuery languageSimplex algorithmSubject indexingCore dumpProduct (business)Set (mathematics)Client (computing)Computer animationLecture/Conference
Process (computing)Thread (computing)NumberServer (computing)Task (computing)Game controllerGraph (mathematics)SoftwareResultantDependent and independent variablesConnected spaceArithmetic meanMereologyFront and back ends1 (number)Lecture/ConferenceComputer animation
CompilerWebsiteClient (computing)NumberTask (computing)Front and back endsProcess (computing)Cartesian coordinate systemMultiplication signSynchronizationDatabaseDependent and independent variablesMachine codeCASE <Informatik>Formal languagePoint (geometry)Cycle (graph theory)Message passingSystem callLecture/ConferenceComputer animation
Cycle (graph theory)Standard deviationType theoryFormal languageWebsiteCompilerFluid staticsMachine codeProjective planeContent (media)Function (mathematics)Extension (kinesiology)Endliche ModelltheorieMathematical optimizationMereologySemantics (computer science)Disk read-and-write headPhysical systemNumberBeta functionMachine codeModule (mathematics)Lecture/Conference
Machine codeInteractive televisionParticle systemOpen sourceWordLaptopWebsiteComputer animation
IntegerOperator (mathematics)SummierbarkeitParameter (computer programming)Type theoryVariable (mathematics)Statement (computer science)Decision theoryFunctional (mathematics)Vertex (graph theory)Event horizonMachine codeRight angleFunction (mathematics)Object (grammar)Strategy gameSign (mathematics)QuadrilateralMultiplication signWeightData typeMachine codeMeasurementExecution unitWebsiteComputer fileFamilyCellular automatonLibrary (computing)Different (Kate Ryan album)PlanningPattern languageWordMathematical optimizationMereologyLaptopAngleDirection (geometry)Extension (kinesiology)ResultantSet (mathematics)Declarative programmingCoprocessorInterior (topology)Electric generatorLoginComputer animation
Functional (mathematics)PlanningInsertion lossFluid staticsMachine codeConnectivity (graph theory)Message passingCausalityData typeComputer programmingVariable (mathematics)SineModule (mathematics)Digital electronicsMathematicsRight angleDisk read-and-write headDeclarative programmingEndliche ModelltheorieSign (mathematics)Extension (kinesiology)Compilation albumObject (grammar)Combinational logicCurveTraffic reportingMultiplication signWritingSemantics (computer science)Fitness functionComputer animation
Sound effectStandard deviationMemory managementRun time (program lifecycle phase)Module (mathematics)Machine codeMessage passingFunctional (mathematics)FreewareEndliche ModelltheorieSpeicheradresseInformationLecture/ConferenceComputer animation
Variable (mathematics)Functional (mathematics)MassSign (mathematics)Bridging (networking)INTEGRALObject (grammar)MereologyEndliche ModelltheorieRun time (program lifecycle phase)Order (biology)Wrapper (data mining)Module (mathematics)LaptopLecture/ConferenceComputer animation
Form (programming)Machine codeParameter (computer programming)Exception handlingDeclarative programmingOrder (biology)ResultantRun time (program lifecycle phase)Resolvent formalismCanadian Mathematical SocietyTheory of relativityDimensional analysisComputer animation
Bit rateException handlingPoint (geometry)Run time (program lifecycle phase)Semiconductor memoryMultiplication signError messageMachine codeFunctional (mathematics)INTEGRALData structureResultantCalculationRecursionData typeLibrary (computing)BenchmarkGroup actionObject (grammar)NumberRoutingRegulator geneLecture/ConferenceComputer animation
Computer programmingNumberBitData structureArray data structureSeries (mathematics)Loop (music)Lecture/ConferenceComputer animation
BitFormal languageLibrary (computing)Vector spaceAttribute grammarSubject indexingEigenvalues and eigenvectorsRepresentation (politics)Sparse matrixCASE <Informatik>ResultantSummierbarkeitParameter (computer programming)Electronic mailing listTupleCartesian coordinate systemFunctional (mathematics)Normal (geometry)Lecture/ConferenceComputer animation
Functional (mathematics)ResultantInstance (computer science)CoroutineCondition numberProteinRevision controlWebsiteDivision (mathematics)Right angleGame theoryIntegrated development environmentIncidence algebraData conversionLevel (video gaming)Function (mathematics)PRINCE2Sheaf (mathematics)MappingNumberLoop (music)Machine codeOcean currentOptical disc driveData dictionaryLetterpress printingType theoryDifferent (Kate Ryan album)2 (number)1 (number)Lecture/ConferenceComputer animation
Multiplication signNumberResultantParity (mathematics)Revision controlDifferent (Kate Ryan album)Event horizonData conversionMassFunctional (mathematics)Cycle (graph theory)Lecture/ConferenceComputer animation
Computer animation
Multiplication signState of matterGame theoryNumberLecture/ConferenceComputer animation
IntegerStreaming mediaNumberLetterpress printingBitBinary codeElectronic data processingData streamRevision controlHoaxOverhead (computing)Integrated development environmentImplementationFunctional (mathematics)Object (grammar)Electronic mailing listPattern languageNegative numberInsertion lossDatabaseFundamental theorem of algebraType theorySoftwareTouchscreenCellular automatonBuildingLoop (music)Spherical capPauli exclusion principleBus (computing)Machine codeProgrammschleifeLecture/ConferenceComputer animation
Revision controlPairwise comparisonFunctional (mathematics)Pattern languageDifferent (Kate Ryan album)Multiplication signComputer animation
Data bufferData conversionInversion (music)Pairwise comparisonGoodness of fitIterationLevel (video gaming)Multiplication signIntegerClosed setTerm (mathematics)Student's t-testComputer animation
Lecture/Conference
Right angleWebsite1 (number)Point (geometry)Cycle (graph theory)Standard deviationArmLecture/Conference
WebsitePoint (geometry)NumberRight angleProcess (computing)CASE <Informatik>ArmBitData bufferCycle (graph theory)CoprocessorLecture/Conference
Directed graphBeta functionWeightWebsiteMultiplication signProduct (business)Single-precision floating-point formatMathematicsLevel (video gaming)Pattern languageStudent's t-testLecture/Conference
Wave packetOrder (biology)QuicksortType theorySocial classCombinational logicMultiplication signPattern languageVirtual machineFluid staticsCompilation albumObservational studyComputer animationLecture/Conference
Lecture/Conference
Transcript: English(auto-generated)
We'll be giving us this talk and there will be time for questions at the end We are delivering this talk together with Stefan
So we just thought that instead of having two standard talk that we give why don't do something spatial this year Why don't bring you some fresh material? So we tried our best to give you some new use cases newer examples of the tools that you might already heard Of many times so we are really happy to present you today
How can we use as in KO and site on together to make more efficient software? so Let's start. We will just begin with the introduction because we see people are still coming so Stefan yeah, why don't you introduce yourself?
From Known from from the project Developer since 2002 Kind of an active member of the Python community especially the one in Germany I'm working for scooby together with
Yeah, so I meant on you see I'm also a Python developer. That's my main job I'm also organizing by con de so I'm using this as another chance to remind everyone that yes It does exist this year. It's in October. So go on by condo de and see and come we are so happy to have everyone there
And let me give a credit to the cool company where we're working on right now. So Scooby is an e-book flat rate e-book subscription service that operates for quite a while It has a coolest deal in Germany and it's available not only in Germany, but worldwide. We have a lot of books We have a decent
Price of $9.99 per month and people love it because they put five star ratings to it That's of course Advertising but what is even more relevant today is that the stuff that we are showing you today is used in scooby At least partially so we are Python based backends we are developing it from
Day to day on daily basis and the stuff that we are presenting you is tested in production. So yes Let's start about this talk. How will it be structured? So we start with the introduction to async IO just to have everyone on the same track What what is it about and what will be the examples about today?
Then we will go to site on topic and have a brief examples of what is it about and how it does work Then we will show you how to use that together and how you can benefit from them from that And we'll show you some practical examples
Easy ones more more complicated ones and then of course, we'll have cool questions from you. I hope so Good. So first async IO Who have you guys used async IO? I think that yeah, we do not need to stop too much on that because it's such a buzzy topic, you know
It's represented very great on this conference. So I Think we just have to go very briefly about it. So everyone is on the same track So async IO, it's finally finally a default tool in Python starting from 3.4, but backported to 3.3 That we can use for the asynchronous network
Communication it gives us tools that have been already available in different libraries It was there forever and twisted. It was there in tornado And there and there was async core as well. But this thing here is sort of a common tool to do this sort of work I'll just
Switch to another notebook quickly to show you some graphics the background
Just a very very brief recap. Why do we need this tool today? Why are we speaking about it? So Let's take a simple case of a synchronous processing of some requests. So
Requests are on the left Processing so our back end is in the middle responses are on the right. This is a timescale. So the time is going down This is a traditional stuff that everyone was taught in the universities. So we get the request. We are processing it We throw the response out then we get the next request. We process it. We throw the response out
Synchronous standard and simple More realistic cases though that we have more than one request coming During the processing of the first one and they just have to wait we receive through requests But we can do just one task at a time. So we do it we throw responses
As we are completing our tasks time is going the same thing Then the deal about this thing is that most of the time at least in the IO bound applications like Most of the websites most of the web services most of the database Applications are just idling waiting for IO So this time is something that our CPUs are wasting and we could save that
If we could do other stuff in the meanwhile while we are waiting on the blocking resource So I mark here. This blocks is waiting. Let's say the database some other external API whatever and Synchronous execution model lets us to save this time by switching between tasks
So we have same three tasks one task two and task three but in this scenario, we work on task one, then we do blocking request to the database and We can work on the other things in the meanwhile, so we jump to the task to right away When task two is done It's a short one
Then we go back to task one because we got a reply from the database and we work on that and so on So we jump between tasks That's how we can cut the idling and waiting on the IO And that's how we basically in most of the internet related applications can save on the total execution time of some tasks
So what is the deal about this in KO with this is first of all? It's an event loop known as reactor from twisted like 12 years ago was already there So again, it's nothing new. It's just a standard way to do that. What I'll look does is essentially it's It's a tool that is managing the network events and it knows which event is related to which code in our application
So it will be remembering when data available on particular socket Which piece of code should it call and then when the piece of codes? Gives the control back to the IO loop then it decides also which piece of code should be executed next
So it does this jumping between blocks of code and it does the callbacks then Another thing it finally gives us a common future class It's same interface basically almost same interface as concurrent futures that we had before It's also pretty similar to tornado futures that we had and there are libraries that can convert one future into another
So you can mix the frameworks And what is the future just a quick reminder, it's so it's called deferred and twisted the placeholder for some results of Probably network operation that is not available yet, but will be available soon We're using it as a link to the future result of some operation to not block and wait but to do other things meanwhile
And finally coroutines again, it's quite similar to coroutines that we had in tornado but Not only the Coroutine mechanic mechanics itself is updated but also the declaration of coroutine is updated
Starting from 3.5 so we can use a sink away. Oops a sink away syntax instead of yield from syntax That is sort of fancier way to do that and site and supports it will have Deeper review of that. How can we use that together soon? Let me just quickly finish with a sink a introduction first
Yes, so coroutines as I said is based on the generators can be Declared with the yield from syntax or with a sink depth syntax. It's
Function or a generator that can be suspended and give the control to other coroutines in the meanwhile A Very simple example that I show you just to get started is we use a new fancy I think depth syntax we do some processing then we want to do the blocking operations. So we use a sink HTTP
Clients to fetch something for us fetching is a blocking operation. So I think it should be climbing will Return us the future not the real result. So we don't wait then we use await syntax or yield from syntax to Point the I you loop that okay. This is something that we don't want to wait give control to other coroutines and just
Resume this coroutine at this place when the result is ready on the socket And then when we have it, we do more processing and return the result easy
so finally we are getting closer to the Do the thing that we will be showing you today about How can we go? Even further in optimizing our asyncio tasks
So on this graph what we are showing you is some front-end server that we have front-end Let's say it's just some piece of the software that accepts network connections and It gives the tasks to back end part that is doing some background processing for us
I think you is running here on the front server It's getting request number one. It does some processing and then okay, we need Some data from the back end. So we do request to back end back and does some processing Meanwhile, we have request to that is going to the front-end server
It is started it is executed And then I think here gives control again to the request number one because this result is already available here What we see here is that even though request one has begun before request two. It will only get response
Afterwards, so it was waiting longer than it could be waiting because the result from the back end that we need Is already available here at this point But because there was a request to coming in in the meanwhile and we have just one thread in which we execute everything we have To delay the processing of the request one even further. It can get even worse if we have a request three
So I think here decides which task should be done next and if at this point it decides, okay we have The request number three and we have data available from the back end here. Which one should we do next? It's good decide
Okay, let's work on the request three and then request one is delayed even further so even though coming in first it will be served last and This and some applications can be quite critical because we are not fair in the way we are dealing client requests here
and The most straightforward thing obviously what we can do is to make this times shorter So the shorter are the times that we need for processing some data The faster we can give responses and also The more flexible we are in switching between the tasks that we are currently executing
So at this case by a shorting the processing time on the asyncio side twice We see that even even though the back end could take the same time to give those data that we need Even though we still are way better in Serving the front-end request because we are more efficient in switching between them
And so our responses will be available faster And as you probably already guessed How can we reduce the latency how can we How can we process asyncio tasks more efficiently is only only if we reduce the task
the time it takes to Process every piece of the code that is there on the sync of your site It's not database because database is probably the blocking response. It will be processed in some other place It is some Usually Python code that is taking time to be executed and here's the site when comes in
Now let's Stefan give you introduction about site and then we talk about how can we optimize? I think your task is that then you 10 minutes intro to site on as compiler in this language
Well site is a compiler. It's actually the most widely used static Python compiler out there It's been to the early days of site on when it was still called Pyrex We're back in 2000 and the site on project itself started in 2007. So it's actually a very old piece of code
It's nowadays it's a major part of the scientific Python ecosystem, so People are writing tools with it They allow you to do lots of number crunching Numpy has site on codes in the code in its Pandas, for example many many well-known tools and beta crunching tools are written inside on
and what it gives you is it's takes a piece of Python code or also a piece of Titan code with which has an extended syntax and outputs C code from it generates C code which is compilable
And this is the C Python C API. So you get an extension module which you can import And then you just use any other module in Python and said it extends the Python language whenever you want to do optimization optimize your code and And interact with external C code external C++ code
it gives you that entirely for free because you know, it compiles down to C so talking to C code is needed and That's one of the cool features. It's open source. You can find it on site on org We are on github. So if you have any cool ideas what we can do better, just talk to us there Okay, so site on in 10 minutes
Okay, so Here's an ipython notebook or Jupyter notebook The first thing you would do is you would say a lot excite on that adds the site on magic to the ipython notebook
And allows it to execute the site on code already. So it's a very interactive way to To get a compiled code into your ipython notebook. What am I using here? So I'm using Python 3.5 and the latest Python release Which I released a couple of days ago Just for the conference
And Then what you can do is you can take an ipython cell and say this is compiled type angle Okay Actually what I'm doing up to here is and this is just plain Python and I could just use a plain Python code and Say please compile it for me
But here I'm already using the extended site on syntax for the set for optimization for using C data types in Python code Which siphon allows me to do? So what's this doing we have a little function at one which is you know takes a value adds one to it I Do the same With two values some n two values x and y and here is already
Part of the syntax that you can see it allows you to it allows me to declare the to declare C data types use C data types for my variables and Then what Python will do is it's going to optimize the code for me
Okay So it sees this variable is actually C integer so I can use native C operations on it and that's as fast as your processor goes As opposed to as fast as Python can deal with objects Which is way faster? Okay So this is how the syntax looks like I can declare argument types. I can declare variables with this C def statement
So I'm I have a global variable a here Which is also just a C int and then I'm using it in my function here adding it To very good X and since both X and a are C integers The ad operation will be run by a simple processor CPU ad operation
Okay The nice thing is you don't have to care about these things I can you just declare variables and Python will generate efficiency code for you, which usually does the right thing
As a way for for Helping you understand what the Python makes of your code. I'm not just saying your site on the site on cell I'm saying site minus a which means annotate take my code and annotate it for me
You tell me what you think about it, and this is what siphon gives me Outputs a little piece of HTML and tells me okay This is what I've seen in your code and when I click on it, this is what I'm doing here So there are a couple of operations and here I'm seeing X is an object variable
so the Operation it does here is actually An operator Python object operation and down here as I said It's taking two variables in plus sign in here. So that's a direct C operation so you can click through the code see what it gives you and Then take that as a hint where you have to touch your code where you have to optimize it
What you can change about it Okay, so executing it some all works as we take that I'm just getting the functions as I would get them get them in Python I can just call them as they are and they get the right result Okay
Functions so up to not only defined Python functions Python has a couple of more function types because when I'm interacting with C code is often necessary to Define C functions directly And second allows you to do that by saying see death in front of function set of death
And then what you get is a plain C function a static C function Which can also pass around into C code as a callback for example Which is visually callable and so on so forth. So that's It doesn't use Python for all semantics anymore. It's not an object. It's just a fancy function
And second allows you to do that by just, you know, exchanging death by C death Okay, couple more things So You can do all this Also from a Python module you can just write plain Python code
Instead of writing a certain module and you can say I take this function compile it for me and then the compilation will actually Occur At import time it's going to take the function Analyze the code for you compile it for you and replace the function by a compile function okay, so it's just
Just a decorator that you can use that it's kind of like did compilation in the sense that it's still compiling steadily but at import time nice feature Okay, how do you interact with C code so far you've only seen that you can use C data types C variables in your code
Here's An example for Using external C code. What I'm doing here is I'm taking the math functions from libc and I'm using them in my code. So I'm taking the sine function. For example, I'm using the declaration for pi
Divide in pi by 2 taking the sine of it and printing it. Okay, this is often how a syphon code actually looks like So there's some C code being involved some Python code some Python objects being there and you can really mix them freely as you see fit Okay, this is just you know, use this use that it's all there for you at the commit tips
This is how How Memory allocation next example, you can use milk and free in in Cython They're just the usual The usual libc functions that you can use here often people prefer to use Python memory allocation
Instead because then, you know, the Python runtime knows about it and the standard Memory allocation in certain modules And
It's it's just plain C functions You can use them from your second code The nice thing about it is that you can also pass C functions around you can obviously pass them around into C code But you can also auto read them to pass them into Python and this is what I'm doing here I'm taking the mass sign function. For example, I'm just assigning it to a Python variable and then syphon goes
Okay, that's a C function In order to turn it into an object, I have to wrap it so it wraps it for me And then makes it part of my module API. Okay, so That's that means that I can now Call it directly from from my Python notebook So I can take that function here and call directly into the libc sign function
Through an object wrapper that's type-in generated for me, but just you know doing an assignment Really nice feature Okay, here's a more involved example New integration
And what I'm doing here is I'm taking a piece of code And Here in my second code. I'm You know, it's not with a couple of declarations in order to use the C API and then here
I'm instantiating lua runtime compiling the code I get calling the code converting the The result arguments Then cleaning everything up One thing you can see here is I'm using try finally. So this is exception based whenever something goes wrong I just raise a Python exception
Even though I'm you know, I'm deeply into calling C code. I can do that at any time doesn't matter Even if the creating the lua runtime goes wrong, I can just raise a memory error because No memory And That's all I have to do. It's just a couple of functions
I can call I can call them directly from my siphon code. It looks a lot like Python except that what I'm calling C code Okay, here's my lua code I'm passing it into the function and It gives me the right results. This is just a recursive Fibonacci
Calculation here. I can use time to benchmark it as well looks kind of fast enough Okay This is how I can talk to external C libraries. Cython has wonderful integration with C data structures C data types
It makes things a lot easier than if you were writing all this in C Who of you have been using C before in programming C? Quite a number of people Do you love it? Yeah, okay Ten minutes gone
So I'll just do a tiny bit more here so this is how Cython deals with C arrays You can do slice assignments. You can copy by value You can loop over them. So it's everything you would expect from Python
just you know using C data structures and One last thing using C++. Who's been using C++? A bit less than those using C Now C++ has the reputation of being Kind of a difficult to use language
It's not from Cython. So if you use it from Cython, it's actually very beautiful because you know, it's also object oriented So it feels a lot like Python When it's done the right way, so I really recommend using C++ from Cython Rather than you know, writing your application into C++, which is
Which you can do but I wouldn't recommend that This is an example how you use a C++ vector from the standard library So I'm getting a couple of values in here as a tuple Assigning it to the C++ vector, just copy over Then I can do
Indexing here, normal Python indexing as you would expect. I can iterate over the vector Do intests Pass the vector into some external C++ function and pass it back And the passing back a vector into Into Python, basically what it does is it just copies it into, you know, the obvious Python representation
Which in this case is this. Okay, so it automatically copies it over so that you can pass it back over as the expected Python result This is how you use it. It has an argument getting back a list Okay, so that's like, well, Cython in 11 minutes. Okay
Finally finally we have 15 minutes more to show you how it actually works together Let's get right into the code so
First we just have a little little helper function that will run something for us with asyncio We get the instance of the IOLOOP. We run the coroutine with that IOLOOP. We return the result Just a helper method So to show you that Coroutines that will be made with Cython are totally compatible with ones that you would do in native normal Python
Let me show you the following example. So we use the fancy async def syntax now and This is just a very very basic function that will add one to Something whatever you feed it in so you give the future in it will await for that future
it will have result then it will say that it will print that result and just for us leave a remark that this was Done with Cython and then it will return this result plus one then Async def one will just return us one obviously, but we want it to be a coroutine. That's why we define it like this
Showing you that it runs we Generate one then we add one and then we add one more. This is a The result that we see we printed one two and the output at the end is three this is Cython
the same thing but Now it's done in pure Python. The only difference is that I say here Python adds so that you see It's actually run with just Python then you see that here
In this example, what they do is I generate number one Then I use Python function at one and then I use the second function function add one And then I run it all on the IO loop and from prints
We also see that the first one was executed with just Python second one with Cython which we could even not notice So it's totally easy to integrate It's easy to mix you can write Cython then you can write in Python then you can call it one from another and it still works Next example just a simple ping pong game. So we will make it a function
Coroutine in Python that will do pink and then we will do similar in Cython that brings pong We will call them the Cython version so we use We do the first definition with Cython it's decrement by one function we will have a map of coroutines
just Some simple dictionaries that will say that First we we have the Cython version then we have a Python version. It's actually a type We pass this mapping in
This condition is just not to print too much so to switch printing on and off based on the show variable we Print pong if it's a Cython coroutine and we print ping if it's a Python coroutine The same thing
But now it prints ping because it's the Python version. The code is simple. You see we just pick the right Coroutine based on the current value and division by two if it's odds then we do Then we call one coroutine if it's even than another one here we have the result ping pong ping pong ping pong
Cython Python Cython Python Cython Python Easy, but what for we did this talk at all is to see if it actually makes sense to mix them if it Gives us any advantage in the speed. So now we will finally time it we
We call the same thing first. It's just a Python version. We see that The result is three four three ms Then we call the Cython version It's one eighty three ms, which is like double faster with no effort at all. Let me just remind you What is the difference between two two functions?
Cython Python Difference is just this and we'd make it double as fast and then just to have it The third example is we mix it so we use for odds numbers Cython for event numbers Python
We time it and the result is somewhere in the middle so it's It's actually not yeah, it's actually it's actually worse. Yes. Let's just run it again You also see that it's light. So now it's sort of in the middle now now it's better. Yes
Okay. Now I have to run them all Otherwise you can think that we made it up. Yeah. No, it's fast. It's fast It's how fast yeah. Yes. Yes. So you see it's still double faster
It makes sense If it all you need is just to put percent percent Cython and you get the double as fast then it's worth it I guess Is buzz example, I'm not sure if we have time for that
Game So that's what we implement in here
Okay, so what we get in is a stream of numbers it's actually binary encoded numbers so four bytes per integer As a network stream, and so I'm just unpacking those Going through the numbers collecting my fist bus values and then
Okay, so a bit of overhead because I'm unpacking stuff here There's a Python implementation of the whole thing and what you can see down here is I'm running through the values I'm using the array object for unpacking everything run through the values here
Appending fizzbuzz fizz or the you know, it's nothing not a physical bus then I just Take the original integer value and then I call back which allows me to pass on the values Here you can see an async for loop which
You know, it just continues running whenever there's data as really nifty feature of the venue 492 cap the new async Async await pep The last two data is in data processing using just follow. Okay, really nice. So Next thing I'm going to do is I'm taking this code verbatim
Copying it into a Python into a syphon cell. That was Python code. Just taking it there renaming it So, you know, I have two functions So I just run the Python version and run the syphon version and get compiled And then I'm setting up my data stream here, it's just kind of a fake data stream that chunks along
Into the stream the chunks so that I can do, you know loops processing Running that here I'm building my data stream. This is what I'm expecting for the first 18 values one to within this fizzbuzz and so on and
Here's just to show you that it works Works in both versions and then here's a Performance comparison between the two and calling the syphon function on the Python function and you can see what the performance difference is
Version runs in seven milliseconds version takes twice as long and then depending on chunk size the Timings differ, but it's usually something like twice as fast just by compiling it. Okay
Then the next thing I did was I rewrote the whole thing in a more, you know, seeish way I'm iterating. I'm running through just the data buffer at a C character level Here doing the same thing in a more seeish way by just casting
You know four bytes to an integer which is faster than Doing the array and the Python array conversion Yeah, so that speeds it up and when I run that thing then What I get in comparison is
It's another I think it's another 40 times or 40 percent faster here So what again overall is some close to three times faster For the inversion
Yep, I think that it's a good time for questions. No don't run
Can you hear me oh, yeah, I was just wondering if you're using this at work and what you're doing with it
If you're doing this technique at work and what you are actually doing with it You mean the using site and together with asyncio exactly Oh, we are using asyncio there. We did not compile I think yet
Cython Pieces there, but we played with it to make this talk and it works That was more the idea of the talk right to convey that, you know speed is important when emeritus you can use satin for it Thanks Okay
Yeah question Am I right in thinking that this also worked with Python 2.7. I mean if you use site on yeah so the problem is the The async def syntax was only added in Python 3.5 So it won't work with Python code, but inside of Python actually works. Yeah, that's the major selling point for me
So Just one more question Can you tell me about numpy integration? Can I use this to cast in umpire race? How how well does that work with site on?
The question was how well Extremely well, like everyone uses it For I said cycle is what is used a lot in scientific Python and we have special syntax in Python that allows you to say this is You know data buffer This is how it looks like and then you can just iterate over it run through it as fast as your processor can
It's one of the main use cases for site on. Yes Alright are these I think our features in site and already ready for production or is it like experimental features?
Like in beta or something So the async async await support in site on was developed at the same time as the Python support for a sink in the weight There were a couple of changes recently, which we followed but they're at the same level now, so they were equally well
And the fun thing is they actually influence each other so while the Python support was being developed we developed our second support and so we had an impact on how they did it and We did it and so like both Improved by working together. I Have a little final note that if you have any interest in getting more
Into detail of site and all or all facing care separately You are very welcome to the trainings that are tomorrow and the day after tomorrow that will cover these two topics really in detail
Thanks Thanks for the talk I was just wondering if if siphon could take advantage of the Python three type hints in order to compile Yeah, so that's a big topic. We were actually so I discussed that with
Deal a couple of times and the intention of the Python type hints is Not really something that's meant for study compilation and it doesn't really help us So it's it's meant for type checking for annotating Apis for making them machine understandable basically, but it does not help in compilation
And that's why we're not currently using making any use of it