PyCon 2014

Analizando el rendimiento y consumo de memoria de la ejecución de código externo con Python

Christine Spang  · 




Extracto de la transcripción automática del vídeo realizada por YouTube.

okay everyone it's ten past twelve time for the next talk the third and final talk of this session before lunch we would like to introduce you to Christine Spang and to tell us about ffi and why you should use FF I rather than subprocess hi everybody I'm

Christine hahaha hi first things first I want to thank you all for being here in this room I've been a part of the Python community for a number of years but this is my first time on stage and I'm really really excited to give back to a community a

community that I really really love so before I jump into my material here today just a tiny little bit about me I got my start in the Python community in Boston after I graduated from MIT in Boston I was a co-founder of the Boston Python workshop and it was

involved in the Python user group there and also use Python in my work at a company called k splice and now I moved to San Francisco and served a company called inbox if you want to learn more button box you can talk to me later but that's not what this

talk is about a tin box and in my personal hacking we love Python we're using Python for everything pythons a really great language for figuring out what you want to write and writing it quickly so this talk is about the times when you're writing Python

but you have some other code that you want to run that's not Python sometimes is not a library available to do what you want or sometimes it's not fast enough sometimes you just want to be able to quickly show it to some code that already exists so

we're going to talk about these two different paths to running external code via Python the first one is running an external binary from Python and the second one will be wrapping a C library from Python and so this kind of stuff the system's level

stuff is very platform dependent so I want to get it out there from the get-go that i'm talking about C Python and I'm talking about Python 2.7 and I'm also talking about linux so there'll be a few details that are specific to these particular

platforms but the general principle is definitely our crops platform you just need to like figure out the exact details I have to do your platform all right the first part of the talk why you shouldn't shell out and sometimes why you should so say I am

so at work I am we have to I'm building an email platform and email it consists of text and it turns out the text is written in every single language out there and especially email there are lots of different ways to represent that text to a computer and

these are called character encodings and it turns out that Python only supports a subset of all the character encodings out there out of the box I suspect this is for portability reasons it has its own implementations of character encoders but it doesn't

support everything and if you want to build some software that is able to handle email from everywhere in the world all sorts of crazy spam stuff like that you need to support all of them so I ran into this problem where I wanted to be able to decode text

at Python didn't support so the first idea we had here was to use this this program called icon which is pretty much the standard way the standard library for dealing with text conversions on Linux platforms and there's a there's a command line

program called icon that you can use on the command line and use it to convert text from one encoding to another so if we want to use this program we can write a little script in Python stay will call it convert utf-8 and it might look like this so you see

here is super simple there's really only one line here that's actually doing anything interesting so we just were parsing some command line arguments and then there's one line right at the bottom here says sub-process check call and then we we

see our icon command line program and all the all the arguments that you would send to that on the command line so this is an example of shelling out and by shelling out i don't mean using a shell actually there's not necessarily a like a command line

shell in volved here though there are some arguments to the subprocess module that can make you pass your command through a shell first I'm just talking about running this external program from from Python and this would obviously usually be part of a

bigger system but I have just cut down the example here to make it really simple all right so what's actually happening here this is one line of code but it turns out there's a lot that's happening behind the scenes and we want to find out what's

going on so the first thing i might do when trying to figure out what's happening in a particular line of python code is use the documentation so python has these great introspection capabilities so i could just call help on the function and i've shown

right here what exactly is the help string for check call and if we can take a look at it and we see it says Ron come in with arguments wait for comment to complete okay so it doesn't really tell us what's going on it does tell us how to use it which

is great that's all we really need to know but I want to know more and in the case that I want to know more I want to know exactly what's happening with a piece of Python code what I'm going to do is I'm going to read the source so since pythons

open source I can just take a look at the source code for it so let's do that right now so yeah I Han here yeah just pointing out one thing if you want to find the source code for any particular python module and there's a Python standard library module

called inspect that will help you with this so you might have used like that Dunder file attribute of a module an easier way is to use the inspect module because the under file it it it doesn't necessarily always have the right thing in it that you want

and also will show you that the compiled python file and not the the source file so you can import this this function gets source file and then you can call that on your module and it'll tell you where the source code is so we can pull up the source scared

[ ... ]

Nota: se han omitido las otras 3.025 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.