Latest / Talk Python To Me / #486: CSnakes: Embed Python code in .NET
Transcript
- 0:00If you're a .NET developer or work in a place that has some of those folks,
- 0:03wouldn't it be great to fully leverage the entirety of PyPI with its almost 600,000 packages
- 0:09inside your .NET code? But how would you do this? Previous efforts have let you write Python syntax,
- 0:16but using the full libraries, especially the C and Rust-based ones, has been out of reach
- 0:22until CSnakes. This project by Anthony Shaw and Aaron Powell unlocks some pretty serious
- 0:27integration between the two languages. And we have them both on the show today to tell us all about it.
- 0:33This is Talk Python To Me, episode 486, recorded Monday, November 4th, 2024.
- 0:38Are you ready for your host? Here he is.
- 0:41You're listening to Michael Kennedy on Talk Python To Me. Live from Portland, Oregon,
- 0:46and this segment was made with Python.
- 0:48Welcome to Talk Python To Me, a weekly podcast on Python. This is your host, Michael Kennedy.
- 0:57Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython,
- 1:02both accounts over at fosstodon.org. And keep up with the show and listen to over nine years of
- 1:08episodes at talkpython.fm. If you want to be part of our live episodes, you can find the live streams
- 1:14over on YouTube. Subscribe to our YouTube channel over at talkpython.fm/youtube and get notified
- 1:19about upcoming shows. This episode is sponsored by Posit Connect from the makers of Shiny. Publish,
- 1:26share and deploy all of your data projects that you're creating using Python. Streamlit, Dash,
- 1:31Shiny, Bokeh, FastAPI, Flask, Quarto, Reports, Dashboards, and APIs. Posit Connect supports all of
- 1:38them. Try Posit Connect for free by going to talkpython.fm/Posit, P-O-S-I-T.
- 1:45And this episode is brought to you by Bluehost. Do you need a website fast? Get Bluehost. Their AI
- 1:50builds your WordPress site in minutes, and their built-in tools optimize your growth. Don't wait.
- 1:56Visit talkpython.fm/bluehost to get started. Hey, everyone. Before we jump into the conversation,
- 2:02I want to talk about infrastructure. Actually, there are a couple of interesting things going on over here
- 2:09at Talk Python. And I've written them up on our new Talk Python blog, which I will link to both of these
- 2:16articles in the show notes. First, we moved our web hosting over to a brand new hosting center in the
- 2:24U.S. run by a German company called Hetzner. And it's super interesting. I wrote this all up. And I
- 2:30think people who are considering, like, where should we run our apps? Or if they're unhappy where they are,
- 2:34it's worth thinking about. And I just rewrote all of talkpython.fm's code, about 10,000 lines of Python,
- 2:42to move to an async web framework. Which one was it? Well, that was part of the journey. And so I did a
- 2:50big long write-up on migrating from Pyramid to Quart, which is async Flask, all the other frameworks I
- 2:57considered doing that, how I went through the migration, and how it's working. So if either of these two sound
- 3:03interesting to you, check out the links to the articles in the show notes. As always, thank you
- 3:09so much for listening. Let's jump in the conversation. Hello, Anthony. Hello, Aaron. Welcome to Talk Python.
- 3:15Hi, Michael. Hi, thanks for having us. It's great to have you here. Anthony, always, always good to have
- 3:21you back on the show. You're one of the most popular guests, and we'll see where it lands, where this puts
- 3:27you in the list. But you're right near the top of the guest page out of all of them. I love having you
- 3:33on the show. Thank you. And Aaron, welcome. It's great to have you here. So even though you've been
- 3:38on a bunch of times, Anthony, maybe a real quick introduction, just who you are for those who have
- 3:42not met you yet. Yeah, I'm Anthony Shaw. I work at Microsoft. I'm the lead Python advocate. And I do a
- 3:49bunch of open source projects. And I wrote a book called CPython Internals, which is how most people
- 3:55know me these days. That's me. Yeah. So we're going to talk about more CPython Internals or maybe putting
- 4:00Python inside of .NET. I don't know. It's something's going into the internals of the something else. We'll figure
- 4:06out which way that goes. It's a very cool project. Hello, Aaron. Tell people a bit about yourself.
- 4:10Sure. Well, I'm definitely the odd one out here. I am someone who is pretty much never written Python in my life.
- 4:17Millie, I now know a bit more about it, having worked on this project with Anthony. But I'm a .NET developer
- 4:21by trade. I've been doing that for longer than I'll admit on a recorded audio. At least 10 years. Okay, got it.
- 4:29Yes, we'll go with at least 10 years. I'm setting a lower bound for us. That's a good one.
- 4:34I also work at Microsoft. I mean, a team adjacent to Anthony's doing advocacy around .NET. And yeah, but otherwise, I also write
- 4:43JavaScript. So I do everything I can to avoid having to get into the Python space in my time.
- 4:49And yet here you are. And here I am. Yes. Amazing. Yeah. So it's a different world, right? Coming from .NET, that's guaranteed static typing, a lot of semicolons, a lot of ceremony around the types. And then Python kind of says, hold on now.
- 5:03And white space. Python loves its white space. But then again, I have a very soft spot for F sharp. So I'm not like, I kind of get the white space significance aspect of a Python language. But at the same time, I do miss McCurley brackets.
- 5:15Yeah. The trick is, if you use a good editor, you don't really notice it. If you don't, it's pretty bad. So use a good editor. I mean, that's all there is to it. Anthony, what have you been up to? What's new in your world?
- 5:27I've been up to this project for a few months now.
- 5:32Is this primarily what you're focused on?
- 5:34It was for a few months. Yeah. I'm trying to get ready for we've got a massive conference, one of our two big conferences of the year coming up in a couple of weeks. I'm working hard on demos to try and get ready for that. But is that .NET Conf? What is that?
- 5:46No, this is for Ignite. It's the partner conference.
- 5:49Oh, yeah, yeah. Ignite is so big.
- 5:51It's like 15,000 people or something.
- 5:53Yes, exactly. I went to a pre pandemic. I think it was 30,000 people.
- 5:57Yeah, it was in Florida. And there was a whole podcast row. So Microsoft had me out as one of 10 podcasts or something. I thought, Oh, what is this? I hardly ever heard of this. I've heard of build and some others. And I show up like, this is something else. Where'd all these people come from? It's a big conference. It's crazy.
- 6:13Yeah, I'm getting ready for that at the moment. But yeah, I've been working on this project for a few months. And we kind of kicked it off in May. Idea kind of floated around in May at the build conference, which is Microsoft's developer conference.
- 6:25After talking to a lot of different developers and people in Microsoft, we were kind of kicking around the idea of if we looked at this again, so like running Python code from .NET, like how would you approach it?
- 6:38I kind of had some ideas, put some ideas together and then started working with Aaron on a really early prototype just to see like if it was even possible. And then we kind of got something working, chucked it in the bin, pretty much started again.
- 6:51And then redesigned it. And then yeah, we've been sort of fiddling with it ever since.
- 6:56From my perspective, I said before, I'm not a Python person. But as someone in the .NET ecosystem, looking at the breadth of libraries that exist for Python, there's a lot of things that I would have loved to have used, particularly doing some of the AI stuff that we've been doing in projects recently.
- 7:15And then finally, there's just not as many packages or options in the .NET space. So it's always been that like, "Ah, okay, well, if I want to learn this, the tutorial is in Python or the package it recommends using is in Python." So it was always kind of like, how do we bridge that gap?
- 7:30That was kind of one of the early thinkings of just, you know, that's a clear problem space, right? Like a lot of people are learning that, but then going into industry and maybe Python isn't the language that the organization they're working with is using.
- 7:43How do we still have that? A fundamental knowledge that they've gained or the like the tooling they want to use available to them?
- 7:48Well, just think about me. I'm sure you have thought about how much of an explosion in possibility it is if you can say, and by the way, there's almost 600,000 other libraries, including the biggest community of AI based resources that are now, as far as you're concerned, native to .NET, right?
- 8:07Yeah. But from the way you consume it, not the way it runs is what I mean.
- 8:11Exactly. Yeah. Like you mentioned there, nearly 600,000 Python packages and you add that to, there's like 400,000 packages on NuGet for .NET. Like you got quite literally a million possibilities.
- 8:24Yeah, it's amazing. And just for people listening, primarily Python audience, NuGet is .NET's equivalent of our PyPI.
- 8:32Yeah.
- 8:33Yep. Yep. Yep. Okay. So Anthony, you had this idea. Somebody said, "Hey, how would you go about doing this?" And then you were sucked in?
- 8:39Yeah. It was actually one of the sort of lead designers on .NET asked me at Build. They said, "Oh, we've been looking at how you might integrate Python into .NET for a while. Like if you were to approach it, how would you do it?"
- 8:52Mm-hmm.
- 8:53And I kind of went through all the projects that have tried this in the past or there are different ways of doing it and pros and cons of each and kind of the pros and cons of a really low level integration and a high level one.
- 9:05So like a high level one would be stick a FastAPI on top of your Python app and just call it via HTTP REST.
- 9:12And somehow auto-generate some REST endpoints and then make those do their thing.
- 9:17Yeah. And you could use it like an open API spec on top of it and auto-generate a client and stuff like that.
- 9:23So that's like the high level option. And then like the lowest level option is like, you know, you could get into the bytecode level, which I've explored years ago with Pigeon.
- 9:33And yeah, that's a very deep, dark tunnel.
- 9:36It's full of terrible edge cases and corners and sharp edges, huh?
- 9:41And one of the challenges there is that the like Python's implementation is changing so rapidly right now.
- 9:47Like, but just in Python 3.13 and looking at 3.14 now and like all the changes and stuff in there, like it's just continuously changing.
- 9:56So it needs to be a happy medium in terms of how we blend the two together.
- 10:01Right. There has to be some level of disconnectedness a little bit, right?
- 10:05So that Python can do it, have its changes without constantly chasing it, right?
- 10:10Yeah, exactly.
- 10:11So yeah, kind of what we kind of came up with was we would do the integration at the C level using Python C API.
- 10:18There's a way of consuming Python through sort of like an embedded mode.
- 10:23Not many projects do this, but it is definitely possible.
- 10:26So you can spin up a Python interpreter inside another process, which in our case is a net process.
- 10:32Yeah. And I think Blender does that.
- 10:34I'm pretty sure that there's a bunch in the film and movie industry that do that for their automation pipelines.
- 10:41I don't think it's that great.
- 10:43One, I talked to some of the people from the movie and video space, and they, most of them are still running on Python 2 because when they wrote the implementation to integrate the thing, integrate CPython, they're like, well, it's done.
- 10:56And like you kind of said, Python moved on and they're like, well, that's a lot of work.
- 11:00And then we got to, we'll just leave it on Python 2.
- 11:03And that's not, that's not great.
- 11:04Yeah, exactly.
- 11:05So we looked at this and said, we want to be, we want to support the newest version of Python.
- 11:09We want to support the sort of practically old version, which I think you settled around 3.8 was like as old as I was willing to go.
- 11:18And I think we maybe push that forward to 3.9 because there were some specific features we need on the .NET side.
- 11:24Again, there's a lot of changes happening in .NET.
- 11:26Oh, Sharon, maybe you can share a bit more, but like, you know, there's .NET is going through just as many changes.
- 11:32So we've got like two moving, constantly moving platforms and we want to make an integration between the two of them.
- 11:38So let's think about like, how can we do this in a way which is actually maintainable and we can make it, you know, we can test it and we can make it stable as well.
- 11:45Yeah.
- 11:46One thing maybe you could speak to as part of this, people might know .NET as this Windows programming library that you use Visual Studio with.
- 11:55Right.
- 11:56But it's, it's changed a lot since then.
- 11:58There's .NET Core, there's open source.
- 12:00It runs on Linux.
- 12:01There's, there's a lot of different things that have been changed about it.
- 12:04Right.
- 12:04Yeah.
- 12:05And it's a fairly common misconception that we've got is that, that people equate .NET with Windows and like historically that's, that's true.
- 12:11Like that's, that's where I got started is developing on Windows.
- 12:14And I, I'll be honest, I'm still a Windows developer.
- 12:17It's what my primary OS has been.
- 12:20I've, I've flirted with macOS and Linux over the years, but Windows is just where I'm more comfortable with.
- 12:26But I think it's been a bit over a decade now since the, they essentially relaunched .NET was originally called .NET Core.
- 12:35So, so now we kind of have these two streams concurrently.
- 12:38You've got .NET framework, which is the one that is what we traditionally thought of as .NET.
- 12:43It, it ships kind of like with Windows and stuff like that.
- 12:46So it's, it's very much designed around that longer tail support lifecycle and, and, and whatnot.
- 12:53It is fairly stable, but also getting minimalistic investment just because it has that kind of need to be stable and consistent year in year out.
- 13:03Then we have .NET Core, or now we just kind of refer to it as .NET.
- 13:08Just naming things is hard.
- 13:10Let's, let's be honest.
- 13:11But so we, we have .NET and every year a new release comes out and this is like a, like a major framework, like a major revision to the runtime.
- 13:20So .NET 9 is actually coming out next week, at least at the time of recording, it's next week.
- 13:25And this is considered a, like a short term support lifecycle.
- 13:29So if you're familiar with support lifecycle of a lot of runtimes out there now.
- 13:34So like Node popularized this quite a lot now.
- 13:37And Python is starting to adopt it a bit more that kind of you have this yearly rolling lifecycle and, and you'll have long-term supports and short-term support.
- 13:45So the long-term is every even number.
- 13:47So .NET 8 being the most recent long-term support.
- 13:51.NET 6 was the previous one, which is actually going out of support next week at the time of recording.
- 13:56Yeah, being realistic of where people are and developing their applications.
- 13:59Where we couldn't just target Python 3.13 and .NET 9 as the only supported versions, because be honest, that's not where most packages are.
- 14:08That's not where most projects are going to be.
- 14:10So we have to make, you know, some, some calls there.
- 14:12Yeah.
- 14:13You do have to have a little bit of, a little bit of slack in there.
- 14:16Even for me and some of my web apps and stuff, I had to wait a few weeks to upgrade to 3.13 because some dependency of a dependency had something that had been removed.
- 14:25You know, been deprecated for a while and was removed and it didn't work.
- 14:28Like, well, try that again in a few weeks and see how it works.
- 14:30And eventually they, they caught up, but yeah, it's definitely a challenge.
- 14:34This portion of Talk Python To Me is brought to you by Posit, the makers of Shiny, formerly RStudio and especially Shiny for Python.
- 14:44Let me ask you a question.
- 14:46Are you building awesome things?
- 14:47Of course you are.
- 14:48You're a developer or data scientist.
- 14:50That's what we do.
- 14:51And you should check out Posit Connect.
- 14:53Posit Connect is a way for you to publish, share and deploy all the data products that you're building using Python.
- 15:00People ask me the same question all the time.
- 15:03Michael, I have some cool data science project or notebook that I built.
- 15:07How do I share it with my users, stakeholders, teammates?
- 15:10Do I need to learn FastAPI or Flask or maybe Vue or ReactJS?
- 15:15Hold on now.
- 15:16Those are cool technologies and I'm sure you'd benefit from them, but maybe stay focused on the data project.
- 15:21Let Posit Connect handle that side of things.
- 15:23With Posit Connect you can rapidly and securely deploy the things you build in Python.
- 15:28Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto, Ports, Dashboards and APIs.
- 15:35Posit Connect supports all of them.
- 15:37And Posit Connect comes with all the bells and whistles to satisfy IT and other enterprise requirements.
- 15:43Make deployment the easiest step in your workflow with Posit Connect.
- 15:47For a limited time, you can try Posit Connect for free for three months by going to talkpython.fm/posit.
- 15:53That's talkpython.fm/posit.
- 15:56The link is in your podcast player show notes.
- 15:59Thank you to the team at Posit for supporting Talk Python.
- 16:02Anthony, when we opened this, you mentioned there had been other attempts, other ways.
- 16:08The one that comes to mind for me is IronPython.
- 16:11IronPython is a CLR implementation of Python.
- 16:15And that's a pretty big difference right there than what you're trying to maybe contrast it with some of the things that people have done previously.
- 16:23Yeah, so probably three projects, I think, that have been done previously that kind of sit in the same space, but they have different goals.
- 16:30So, I mean, the main goal for our project is that there are loads of .NET developers that have existing applications.
- 16:38They are typically quite, it like sits in the enterprise space, a lot of it.
- 16:42So they're like quite big, big applications.
- 16:44They've got teams working on them and there's a technology in Python that they want to use.
- 16:49And that technology is often a package or a library or an example, especially in like data science now, and a lot of the ML and AI libraries that are floating around.
- 16:59So a lot of the idea for this and the demand from developers was from, you know, we want to use these libraries or we want to use this particular package in Python.
- 17:09And like, how do we import that into our .NET application?
- 17:12So like, that's kind of the angle that we've come at this from, which is like, how can we make the best of both worlds?
- 17:18Like you've got a big, you're like a fast, mature enterprise application in .NET.
- 17:23And it is like a piece of technology that's in Python that you want to use.
- 17:26Iron Python is an implementation of Python written in .NET.
- 17:30And that was done quite a while ago, I think.
- 17:33And yeah, like older versions of .NET, older versions of Python.
- 17:37And it's kind of similar to Jython as well.
- 17:41Like I think I put it in the same bucket as Jython, which is...
- 17:44I would as well. Yeah.
- 17:45It would like, and at that time, a lot of people were making different implementations of Python, the language in other frameworks.
- 17:52And so, yes, you can write Python syntax and run it in the CLR, but you couldn't do something like import NumPy.
- 17:58And that's the difference is that most packages in the ecosystem these days are designed and tested for CPython.
- 18:05And this is all, at least they're tested for PyPy.
- 18:09But all the other implementations are pretty much a no.
- 18:12Like Pandas, NumPy, scikit-learn, like all the data science ML tools.
- 18:17Especially the C interop type of stuff.
- 18:20Yeah.
- 18:21And I've sort of written about this in the past, how, you know, Python's...
- 18:24But one of the solutions to Python's performance problem is that you implement extension modules in C, which is a great solution.
- 18:31Or you use Cython, which is the same kind of thing, which is a great solution.
- 18:34It just means that the ecosystem is more and more tied to CPython as the interpreter of choice.
- 18:39So what I didn't want to do is write a whole new interpreter.
- 18:42Like that goes back to the compatibility problem and the maintainability issue.
- 18:47Like by the time we published it, it would be out of date and we'd just be opening up stuff up for a whole load of work.
- 18:53It's not even about, it's not even a matter whether you can do it and you can keep up with the language.
- 18:58As we talked about, you know, as Aaron mentioned, there's a million libraries you can use.
- 19:02But if you did this, you wouldn't be able to.
- 19:04Yeah, I'm Python is one.
- 19:06Pigeon, we've covered in a past episode.
- 19:08Oh, that's not going to work.
- 19:09Is a P-Y-G...
- 19:12There we go.
- 19:13Is a weird spelling.
- 19:14P-Y-J-I-O-N.
- 19:16No, that's right.
- 19:17I remember now.
- 19:18P-Y-J-I-O-N.
- 19:19This is a JIT for CPython that happens to use the .NET Core JIT engine because that was one of the most pluggable JITs at the time.
- 19:31There are other JITs available.
- 19:32It could have been written in ASM JIT.
- 19:34It could have been written in LLVM JIT.
- 19:37Dino who actually worked on Iron Python.
- 19:39So like he knows this space really well.
- 19:41Brett Cannon worked on Pigeon originally.
- 19:43And then I rewrote it like a few years later.
- 19:46That's a different problem.
- 19:48That doesn't help you integrate .NET and Python together.
- 19:51It just happens to use components from .NET to implement a JIT.
- 19:55That project is effectively dead.
- 19:57Some of the benefits of it have been rolled into CPython in versions 3.10 and 3.11.
- 20:03The things that made it faster.
- 20:05Most of those were sort of picked up anyway.
- 20:07And then the third one is Python.NET, which is probably the closest to the project that we've started.
- 20:13This one has been around for a while.
- 20:16This one's been around for a while.
- 20:17It's a similar thing, but they mostly focus on the integration the other way around.
- 20:21So like how do you call .NET libraries and things from Python?
- 20:26You can call Python.
- 20:27Oh, right.
- 20:28Yeah.
- 20:29A lot of the focus is actually the other way around.
- 20:31So if there was a popular .NET library that you wanted to use in your Python app, you might Python.NET your way to that.
- 20:37And this project has been around for a while.
- 20:39So the API is pretty stable.
- 20:41And we originally looked at this one actually as a potential solution, but found a number of things that they'd kind of tied themselves to in the design, which are going to go away in .NET 9.
- 20:50So it wasn't really an option for us.
- 20:54And also like a lot of the way the C API was used was different to how we wanted to do it.
- 20:58So yeah, that's kind of the alternatives.
- 21:01And so you ended up writing your own because just none of these really match, right?
- 21:05The Iron Python was just never going to keep up and it didn't have this package support for data science mostly.
- 21:12And then the other one was the other way around.
- 21:14And yeah, so well, I guess a good thing to think about is, well, why did you think you could do that?
- 21:21Like a lot of people, a lot of smart people put a lot of time into this.
- 21:24Why did you think, oh, this is going to be, this is going to work well.
- 21:27Do you have a fresh take on it or what were you thinking?
- 21:29I really wanted to use some of the modern benefits of both Python and .NET to do this, to design it.
- 21:37And one of the biggest changes in actually, as we've been implementing this, we're learning 80% of the challenges are related to typing.
- 21:44And Aaron's smiling because he's agreeing with me.
- 21:48Like integrating two different ecosystems together like this is about calling functions, methods, whatever.
- 21:56But calling things is about calling them with arguments really.
- 21:59And it's passing data backwards and forwards, marshaling information and getting things backwards and forwards between different types.
- 22:05Like that's if you integrated, I don't know, some function that you wrote in Python into .NET, you're going to be calling it with something like you're going to be sending it some data and expecting some data back.
- 22:16Now in Python, there are types.
- 22:18It is dynamically typed, but the types do exist.
- 22:21Even if you don't put it in your function signature, there's still instant strings and stuff being sent all over the place behind the scenes.
- 22:28Yeah, exactly.
- 22:29And when we looked at Python.NET, like, you know, you could call a function in from .NET in Python, but you'd have to write a load of code that said like, oh, I think this parameter is probably this type.
- 22:39So convert that to this and then when it comes back, then it's probably this one to try that one first.
- 22:44And so we sort of said, like, now typing type annotations are more popular in Python.
- 22:50What we wanted to do was basically you give it some Python code and you write like a function, which is the one that you want to call and you annotate the function.
- 22:59So you say it takes, you know, has these arguments and they are these types and it returns back this type.
- 23:05And then what we did is wrote a code generators called a source generator for .NET that would in in your project, it would look at the Python file that you wanted to embed.
- 23:15It would see what functions are defined, what their signatures are and what types they take.
- 23:19And then it would generate a function that can call that for you with .NET equivalence.
- 23:24And then it handles the marshalling, like the transfer of the types backwards and forwards between .NET and Python.
- 23:32And like that's most of the project is really about that particular problem.
- 23:36Okay.
- 23:37So it's leveraging the C API of Python kind of talk directly to the runtime.
- 23:42You can write a function in Python, you know, that takes a string, a list and a tuple and returns back a dictionary.
- 23:48Like it's a fairly common thing to do.
- 23:50Yeah.
- 23:51You would give it type annotations in Python and you drop that Python file into your .NET project and you run build again and you would see a type in .NET with the same name, with a function that is the one you just defined and with arguments which are .NET equivalents to that.
- 24:07And you just call the function and it just runs in Python.
- 24:09How complicated can this data exchange be?
- 24:12Can I come up with my own types and exchange them or do they have to be six or seven core types?
- 24:17I think the data exchange is actually one of the really tricky bits, right?
- 24:21Here's a list of a million items.
- 24:23Why don't you copy them all over to a .NET type, run it or copy it over to a Python list, run the thing and then copy the back to a .NET list with one more item in it or something crazy like that, right?
- 24:34Yeah. If you jump to the documentation and then in the reference page, there's a list of the ones that we support at the moment.
- 24:41Oh, yeah.
- 24:42Then there's a list of the core ones that we support.
- 24:44And then anything beyond that.
- 24:46So we do generics, for example.
- 24:48So if you say it's a list of strings, we will give you back a read only list of string in .NET.
- 24:54If it's a dictionary and you type the key and the value, we will give you back a .NET key and value.
- 24:59Now in Python, often it could be an any or it could be a union or it could be something complicated.
- 25:06So you've got the option to say it could be a dynamically defined type.
- 25:10It does still support it.
- 25:12And then at runtime, you can look at the type to see what it is and then convert it into something that you need.
- 25:16I see a lot of read only on the .NET side.
- 25:19And this is the thing receiving the data from Python.
- 25:22I'm glad that you picked up on that because that was something that I made a decision when we're doing the .NET service area for this of it.
- 25:29So that's the thing that I think that anytime we're receiving data back or when we're sending data to is it should be represented as read only just to indicate that if you're calling a function that's been exposed from Python from .NET and it's returning a collection like a dictionary or a list or anything like that.
- 25:47So that's the thing that you're going to do.
- 26:08So that's the thing that you're going to do is you're going to do that.
- 26:13And then you're going to do that.
- 26:31That you're very familiar for .NET developers for doing like creating an array from an existing collection of data.
- 26:39And then that becomes obviously a more mutable data structure or like a list, like the read-only list, for example, that then plugs straight into links so that you can do the query syntax or the expression methods syntax to perform operations on those collections of data inside of .NET space.
- 26:59Kind of once it's being hydrated back out of Python.
- 27:02So I guess you get a lot of these read-only innumerable type of things.
- 27:05You could do link on them, L-I-N-Q.
- 27:07Yeah.
- 27:08I wish we had this in Python.
- 27:09I know we have something kind of like it with list comprehensions, but it's not the same.
- 27:12One thing that I really wanted to make sure that we were tackling with this and to Anthony's point before of the, that you've got two very different type systems that you're working with.
- 27:21And structurally, the way that you write code in Python is going to look different to the way that you write in .NET.
- 27:27So we didn't want to feel like the code gen that we do for you is that we've just made this, it's just .NET casing of Python code.
- 27:37So I wanted to feel like as a .NET developer, I'm working against a .NET interface.
- 27:41So when you work with the types and the modules that you expose out to .NET and we do code gen against, we then generate you a class behind the scenes that is then, we then generate an interface for you.
- 27:55And then that's registered into the dependency injection container.
- 28:00So you then can just resolve that elsewhere with inside of your application.
- 28:03So now as a .NET developer, I'm getting an interface injected into a class that I'm using elsewhere with inside of my code base.
- 28:12I haven't had to go, okay, well, no, I'm calling some kind of like an import or I'm doing some kind of resolve or something that might be unfamiliar to me as a .NET developer.
- 28:24This is what I'm doing is just, it just feels like I'm writing, or I shouldn't say necessarily .NET developer, I'm running C#.
- 28:31And we've optimized this for C#.
- 28:33I said earlier on, I have soft swap for F# and it kind of works in F#.
- 28:39And then similarly, if you're a .NET developer, we optimize this to the kind of development experience you would expect when you're writing C# code.
- 28:47We just so happen to have, you, you haven't written this, some of this C# code.
- 28:52We've just automatically generated it for you and injected it into a dependency injection container.
- 28:57Amazing.
- 28:58So when we're talking about exchanging this data, this is CPython in the same process.
- 29:03Is that right, Anthony?
- 29:04Yeah.
- 29:04How's that work?
- 29:05Python's C API, normally if you call, so CPython is the interpreter that you would use to run your Python code.
- 29:12It is implemented in C mostly.
- 29:15The standard library modules are written, some of them are in Python, some of them are in C.
- 29:19You can write modules for Python in C.
- 29:22And the way that you would do things like create numbers and dictionaries and lists is you use this API.
- 29:28It's a C API.
- 29:29And part of that C API is the ability to spin up a Python in C.
- 29:34You can spin up a Python interpreter inside another process.
- 29:38And that's embedded Python.
- 29:40There's different APIs for doing it.
- 29:42There's a lot of different ways of approaching it.
- 29:44But really what we do is that when you spin up .NET and you use CSnakes, you inject a Python interpreter into the environment.
- 29:53It's not like a separate process.
- 29:55Like it's not, it hasn't got like a ship command line windows running in the background.
- 29:59It's embedded into the process.
- 30:01It's like loading the library.
- 30:02It's running inside the process.
- 30:04And then we have full control over everything in CPython.
- 30:07So we can then import modules.
- 30:10We can convert things, types backwards and forwards.
- 30:14So we're kind of doing a lot of the work that often gets done either in a C extension module or inside the interpreter.
- 30:20So if we want to create a Python number, for example, like an integer from a .NET integer, we do that at the C level.
- 30:27So we can do that like a really high performance API.
- 30:30If we want to look at the size of dictionaries or the types and stuff like that, we do that at the C level.
- 30:36So again, it's super high performance.
- 30:38And then really importantly, well, the typing stuff is really important.
- 30:43The other major challenge with this is, you know, .NET freely uses threading and thread pools and coroutines and stuff like that.
- 30:52Python can use threading, but there is a lock.
- 30:55So you have to be really, really careful.
- 30:58And this is something I don't didn't see like a novel solution from the other projects that are looking in this kind of space about how they would tackle it.
- 31:05Or it was kind of based around like older styles of Python and things have changed so much recently.
- 31:10So we kind of wanted to make sure that you could run this in a multi threaded environment, whether that's like a UI, for example, like a desktop interface or it's a web app.
- 31:19So like one of our demos is like a .NET web app, like API, that each request can run a Python function if it wants to.
- 31:27And they're all running in separate threads in a thread pool in .NET.
- 31:30And you shouldn't have to worry about the GIL.
- 31:32Like you shouldn't have to know too much in terms of what it is and how it works.
- 31:36So we've done quite a lot of work to make sure that that can happen.
- 31:41This portion of Talk Python To Me is brought to you by Bluehost.
- 31:45Got ideas, but no idea how to build a website?
- 31:48Get Bluehost.
- 31:49With their AI design tool, you can quickly generate a high quality, fast loading WordPress site instantly.
- 31:56Once you've nailed the look, just hit enter and your site goes live.
- 31:59It's really that simple.
- 32:00And it doesn't matter whether you're a hobbyist, entrepreneur or just starting your side hustle.
- 32:05Bluehost has you covered with built-in marketing and e-commerce tools to help you grow and scale your website for the long haul.
- 32:12Since you're listening to my show, you probably know Python, but sometimes it's better to focus on what you're creating rather than a custom built website and add another month till you launch your idea.
- 32:22When you upgrade to Bluehost cloud, you get 100% uptime and 24/7 support to ensure your site stays online through heavy traffic.
- 32:31Bluehost really makes building your dream website easier than ever.
- 32:34So what's stopping you?
- 32:35You've already got the vision.
- 32:36Make it real.
- 32:37Visit talkpython.fm/bluehost right now and get started today.
- 32:41And thank you to Bluehost for supporting the show.
- 32:45Python has async and await.
- 32:47C# has async and await.
- 32:49They're similar, but not the same.
- 32:51Yeah.
- 32:52Can I have an async function in Python that is called by C#?
- 32:57No, not yet.
- 32:58Just a task.run, async.io.run to completion sort of thing.
- 33:03And then keep going.
- 33:04Yeah.
- 33:04I would love to do this, but there is no API for it on the Python side to manage event loops and embed an event loop into another process or to write a custom event loop.
- 33:17It's all kind of built into the standard library or was put into another module.
- 33:22There's been a couple of proposals in draft peps, but none of them have been accepted.
- 33:26And if they were, then it might be in 3.14 anyway, which we can't backward compatible support.
- 33:31Yeah, you're not quite there time wise yet.
- 33:33Do you find that async.io event loops and Python are hard to juggle?
- 33:37Right?
- 33:38Like you have to create one if it doesn't exist, but if it does exist, then it doesn't want you to create a new one.
- 33:42But there's not a great way to see if it exists.
- 33:44And if you don't have direct access to it, how do you get like, it's just, it always feels to me a little bit out of touch to fully take control over that and work with it.
- 33:52And it changes between Python versions.
- 33:54Like it gets, it's gotten easier recently.
- 33:57Like there's a, there's like a dot run function now.
- 34:00And whereas previously you have to like get the event loop and create one and stuff like that.
- 34:04But now you can just kind of run the coroutine.
- 34:06It's still tricky.
- 34:07And the idea of basically writing a custom event loop and then giving it coroutine objects and like polling them and looping them from another runtime.
- 34:18I just couldn't figure out the API.
- 34:20I'd love to do it.
- 34:21If anyone knows, then please ping me.
- 34:23Well, uvloop does it.
- 34:26Yeah.
- 34:27Somehow, but maybe they just swap out some pieces of it.
- 34:30Not all of it.
- 34:31Yeah. I'm not sure how, but you can set the event loop from uvloop.
- 34:34So maybe, I don't know, maybe there's a breadcrumb there to chase.
- 34:37All right.
- 34:38So I see in the steps, install Python, put your projects in a C# file, use annotations on your Python functions, right?
- 34:46That's a really important step that you talked about.
- 34:48Install the CSnakes runtime package.
- 34:51So maybe from this point on, let's talk a little bit about some of the steps.
- 34:55So this CSnakes runtime, this is a NuGet package that you put into your .NET project.
- 34:59Is that what I'm reading here?
- 35:01Yeah.
- 35:02So we essentially have two binaries that we ship.
- 35:05One, which is going to do all the code generation for you.
- 35:08So from a .NET perspective, you'd be, it wouldn't be uncommon to have multiple different projects with inside of your solution, which is then like one might just be a bunch of services, which are then going to then talk to a database or whatever the case may be.
- 35:25So you might have that as just a, like, that would be where you would have the code gen that you want to do.
- 35:31So where the Python files are actually getting their C# interop generated for you.
- 35:35So you have one project with that.
- 35:37And then we have a second binary, which is then the runtime to actually like make that work when your application is running.
- 35:44Now we do ship this as a single consolidated package just for ease of use, but that's kind of why we differentiate some of it in terms of like code gen versus runtime because they're different concepts.
- 35:55And one is a compilation step versus obviously runtime step.
- 35:58Yeah. Okay.
- 35:59With our runtime package and the documentation talks a little bit more about this.
- 36:03There's just a few steps you've got to do to make sure that the C# compiler is capable of finding the Python files.
- 36:09So we actually do stuff with inside of the C# compiler pipeline to generate code that is then added to it.
- 36:16So this also means that we can very early on find out if you're going to have compatibility issues.
- 36:22So if something is type of the type that we don't understand or the Python code is syntactically obscure, because what I've learned is that there are ways that you can write valid Python code that is very obscure.
- 36:36And they showed me some, yes, some things that you can do, but please don't do it.
- 36:41And yeah, so you'll actually fail a compilation step rather than fail runtime process.
- 36:45And I think like, again, from a from a .NET developers perspective, like you were used to that static typing that compilation step that that belief that the compiler has done a whole bunch of checks for us to give us code that should just work rather than just like that more dynamic or typing system that you get out of Python or like in my experience, like the JavaScript side of things.
- 37:05Yeah, that's definitely the expectation.
- 37:06the expectation if you're doing C#, C++, if it compiles, at least it's clicked together in a coherent way.
- 37:13Right. Whereas, whereas Python, we're pretty sure we don't know for sure, but we're pretty sure.
- 37:17So in Python typing, you can lie.
- 37:20Right. I could say it takes a list of integers and really what it took was strings, which had numbers as characters.
- 37:26Who knows? Right.
- 37:27What happens if I lie to the C# code gen thing?
- 37:30So it just crashes.
- 37:31Don't do it.
- 37:32Yeah.
- 37:34It does actually just crash when it calls because it has to make the assumption that what you've told it is correct.
- 37:40So in .NET, if I've got a tuple of ints and I give it across and it's like, I actually wanted strings.
- 37:47No, it's not going to have it.
- 37:48It will raise a, yeah, because to get, for example, if it returns a list of strings, but you actually return a list of integers, you know, in order to give you back a list of strings in C#, we first check that it's a list.
- 38:01And if it isn't, or if it's not a sequence type, like it could be a, you could have inherited from this to major and custom list, or you could make something which is a sequence.
- 38:09Like if it's not that, then we will raise an exception.
- 38:12It doesn't just crash.
- 38:13I was joking.
- 38:14And we've got a lot of testing to make sure there's a whole, like a test environment that's around like lying code and type hints and stuff, which are completely invalid.
- 38:23And then for the strings, you know, we need to look at the Python string.
- 38:26And then because Python mostly uses UTF-8 and .NET mostly uses UTF-16, you know, we need to convert from one to the other.
- 38:34And if it's, and if it's an integer, not a string in the first place, then that's never going to work.
- 38:38So we would raise an exception there and say, you taught us it was a string and you lied.
- 38:42But you lied exception is thrown.
- 38:43Yeah.
- 38:44I think it's like a cast, invalid cast exception or something like that.
- 38:47Yeah.
- 38:48With the description.
- 38:49We've tried to raise like the exceptions we raise are .NET exceptions, which are relevant to the concept of the problem that we've covered.
- 38:57But at the same time, it actually will give you, imagine if something failed in the Python code as well.
- 39:02Like you type annotated that it was like a bunch of strings and we, and I know a bunch of ints and we, we gave you a bunch of strings.
- 39:09Well, okay.
- 39:10The Python code is going to fail there.
- 39:12So where we then surface out the Python exceptions as well as like embedded with inside of the .NET exception.
- 39:19So you can, you can actually get down to like the lines of code with inside of Python that failed, which I think is kind of.
- 39:26The stack trace actually includes C#, C#, C#, Python, Python, Python, potentially.
- 39:31Yeah.
- 39:32That's pretty awesome.
- 39:33In Python, if you didn't raise from it, that will show up as an inner exception in .NET as well.
- 39:38So we did a lot of work on exceptions, mostly because we had to like debug this stuff when we're developing it.
- 39:43And it was pretty amazing.
- 39:45We saw the pain that someone else was going to feel.
- 39:48So yeah, you see the, you can see the stack trace, you get like all the frame variables and stuff like that.
- 39:52So things that you would probably want from like a PDB, you can see on the .NET side.
- 39:57So if you're running the debugger in .NET and it crashes in Python, like it or raises an exception or something, you see the Python exception with all the details.
- 40:06And then you see like inner exceptions and stuff like that as well, as well as the locals and the globals and any other frame information.
- 40:13Yeah, I was pretty impressed when I saw that, that how integrated, how much I felt from both directions, kind of natural and not just bolted on.
- 40:21Yeah.
- 40:22Another step is mark these for code for source generation.
- 40:25This feels like a Visual Studio 20, whatever year the last release of Visual Studio was.
- 40:32Aaron, where are we?
- 40:33What's the number?
- 40:3422.
- 40:35Okay.
- 40:36So the reason I bring this up is a lot of people hear Visual Studio, especially in the Python space and think Visual Studio code.
- 40:41Does this work in Visual Studio code or is it just a other Visual Studio?
- 40:45No, it definitely works in VS Code.
- 40:47They sometimes open it in VS Code.
- 40:49And obviously you'd likely just installed the C# extension to get a better developer experience on that.
- 40:54But what we have to do is because the source generator is actually running as they step with inside of the C# compiler pipeline, we need it to know how to find these files.
- 41:04And we kind of toyed with the idea of just to like, just do a directory scan.
- 41:08Everything that ends in .py, we're going after.
- 41:10That's not particularly efficient.
- 41:12And also like if you've just got like modules that are using other modules, like you want to, what's, how do you craft a surface error in Python that you actually want to expose rather than just expose everything?
- 41:22So we're inside of the csproj file.
- 41:26So this is the file that is the instructions on how to compile a more complex .NET application.
- 41:33We can then add nodes to that XML structure to say, like, here's a Python file.
- 41:39It's an annotate that for the C# compiler to pick up so that when the source generator hits it, it hits our steps.
- 41:47It goes, okay, I'm going to find all of these additional files that you've said I need to be aware of.
- 41:52And then I can read them in and it's actually got some optimizations for how like internally it will read that file in rather than us then having to do file IO or anything like that.
- 42:01So it just, it's more in physically aware of them and that it will make those available so that it can then generate out the code rather than, rather than like I said, us having to try and guess what Python files you want available.
- 42:14Like, you know, they have to be in a special folder or they had to be named a certain way or we just directory scan everything that ended with .py.
- 42:21Yeah. You want to be really careful about which ones you're trying to do this to because they require type annotation and you don't want to just expose all the guts of some bit of code.
- 42:31You want to maybe talk about the surface area, right?
- 42:33As .NET people, you have these protected private internal, protected internal, et cetera, things.
- 42:38We don't talk about them in Python.
- 42:40I want to be consistent with that, right?
- 42:41So Anthony, it says last step to instantiate the Python environment.
- 42:47This is, it feels almost like a, an import statement.
- 42:50Then you use it namespace style later.
- 42:53Do you want to talk about that real quick?
- 42:54In order to start like into embed Python into the .NET process, you need to create a Python environment.
- 43:00And there's a few things we have to know, like where is Python installed?
- 43:03What version is it?
- 43:04Do you have a virtual environment?
- 43:06Is it just like the global environment?
- 43:08Do you have like packages?
- 43:10There's like additional things that we need to know that you would have in Python, you would have kind of set those elsewhere.
- 43:16Like if there was a virtual environment, you would have activated it.
- 43:19You know, if you were running Python, then you would have set which version it was and where it's installed.
- 43:24So you've in .NET, you haven't done those steps.
- 43:28So we need you to tell us like which version of Python do you want to, do you want to create and where is it?
- 43:34And we've created a whole bunch of code to help you find it.
- 43:37The uv team actually have been solving a similar problem with like spitting up Python and running Python.
- 43:44So I'm kind of curious to see like how they've tackled it.
- 43:47And we might even try and look at bringing uv in as one of the mechanisms for finding Python for you.
- 43:52Because it's such a complicated.
- 43:53Yeah.
- 43:54It's something I wanted to talk to you about is you've got to have Python installed and you've got to get the dependencies.
- 43:59And yeah, we're not going to install Python for you.
- 44:02Yeah.
- 44:02But with uv, V and V, give it a Python version.
- 44:06It can fetch it.
- 44:07It can fetch it and just run it and it's cached.
- 44:09And maybe you could embed that as a resource at compile time.
- 44:12There's interesting possibilities there.
- 44:15Virtual environment support uses either condor or pip as the package manager, depending on what you want.
- 44:20And you can say, here's my requirements file, and it will go in and pip install them as well.
- 44:25Like it can do all of those things for you because we're kind of assuming that, yeah, you've developing this locally and you're running it locally, but it's going to be deployed somewhere else.
- 44:34So you don't want to have to like log into the production servers and like create virtual environments on the command line and stuff like that.
- 44:41That's like with hopefully 15, 20 years past people doing that.
- 44:45Like, so you should be able to programmatically say this project needs a virtual environment and it needs these packages installed and needs this version of Python.
- 44:54And then you can either use the version of Python that comes with your Linux container, for example, or you can pull in one from somewhere else.
- 45:03For Windows, you can actually download it from NuGet.
- 45:06It's like you can actually add it as a dependency in .NET.
- 45:09And that's a really simple solution for Windows development for macOS.
- 45:12If you've installed it from Python.org, like using the official installer, you just you just tell it that you just say, use like the macOS Python and give it the version number.
- 45:22And then for Linux, it's a lot more complicated because there's no standards.
- 45:27So, yeah, we've kind of support every which way of finding it on Linux.
- 45:31And then once you've kind of put the Python environment into the process, there should only be one of them.
- 45:36Python itself does not want you to ever spin up more than one Python environment in the same like operating process.
- 45:43It will have all sorts of problems if you if you do that.
- 45:45So it's basically put in as a singleton and then you import a module by creating an instance of it.
- 45:52And then on that module, you can then call methods which are the functions in the module.
- 45:57So like if you had a module called demo.py, then in your environment, you'd see like a factory for like effectively like import demo and giving back an instance of it.
- 46:08And then on that instance, you'd have methods which are the functions.
- 46:12And the example you've got on your screen, you know, there's a function called hello world that takes some arguments in a in a file called demo.py.
- 46:19So you just say environment.demo, like make a demo instance.
- 46:22And then on that instance, you can call dot hello world with the arguments and it gives you back the results.
- 46:28So there's no really like in a few lines of code, like four or five, you can spin up a Python environment, import a module run a function.
- 46:35So we'd really try to minimize the border plate code by and then following the patterns that are best for both platforms.
- 46:41So a lot of places you could go with this is really cool.
- 46:44For example, if you if you got a singleton, which is great.
- 46:47And that's kind of how Python typically is one environment.
- 46:49But with the sub interpreters, you could theoretically create these in parallel.
- 46:54But one thing that stands out as maybe a little non expected in this is like you have var module equals env a demo, right where you're creating this.
- 47:03But in Python, this thing is called demo.
- 47:06Would it be a pattern to maybe name the variable the same as the module name?
- 47:11That's probably just more from laziness in the way we've written our docs.
- 47:14Yeah.
- 47:15But what we're probably lacking from because this is obviously docs and just markdown code is that so when you were so we would get back our IPython environment from our dependency injection container and then things like the demo methods exposed there would then return an i demo.
- 47:33I think it is or and that is then the representation of the that demo dot PY file that you've imported.
- 47:40So, yeah, probably.
- 47:42Yeah.
- 47:42So then then all those methods are then exposed on that.
- 47:45So, yeah, a module from it like conceptually, if I'm if I'm thinking about this as a .NET developer looking at it from Python, like, okay, that the module is that Python file.
- 47:54And that's what I get back when I say, like, I want an instance of the interface into that file.
- 48:00Yeah, and it does honestly it doesn't matter how I feel about this as a Python person.
- 48:04It feels about it matters how .NET people feel about this when they look because that's this is C# code.
- 48:09That's their code.
- 48:10That's their world.
- 48:11It just so happens that C snakes make it makes it really nice possible to talk to the Python world.
- 48:16Yeah.
- 48:17So one thing I wanted maybe could spend a couple minutes talking about this Anthony is I saw an example where you you showed integrating open telemetry and ASP .NET and Postgres and all sorts of stuff together, not just a, you know, hello world sort of example, which shows some really cool stuff, but maybe doesn't convince people that they could do cool stuff with it.
- 48:39You want to just maybe talk us through some of the moving parts of that to give people a sense of a more realistic example.
- 48:44Yeah, so we have a another demo, which is a web app medicine in the project.
- 48:49Well, this is kind of what we thought was a more realistic integration example.
- 48:53So you've got a web app written in.
- 48:56Yeah, well, the front end written in HTML and JavaScript.
- 48:59The API is written in dotnet.
- 49:01You're using Postgres as the database.
- 49:04And you've got like a feature that you want to add to your app where you want to use some Python libraries and some Python code.
- 49:11And we were looking at whether everyone loves the weather demo.
- 49:14Just real enough.
- 49:15Come on.
- 49:16It's just real enough.
- 49:17The weather demo is like the canonical .NET demo of when you do file new API projects like here is a weather app.
- 49:24So I from a .NET developer, I feel very much at home.
- 49:27Yeah, you're already on board.
- 49:29Okay, got it.
- 49:30Yeah, we're creating the one that people would expect.
- 49:32Now on the .NET side, we also said, right.
- 49:34Yeah, let's be realistic about what people would want to do, because like modern applications, you would write the API in ASP.net and you would, you know, have API endpoints that you would and routes and configure those.
- 49:47And you would have a probably a relational database like Postgres or SQL Server or something as the storage environment.
- 49:54And then now we support open telemetry in a platform called Aspire, which Aaron can probably explain in a second.
- 50:01So open telemetry is a new standard for like tracing and instrumentation and observability generally of applications written in any language.
- 50:12And in .NET you can have.
- 50:14So for this app, for example, like, you know, somebody makes a request to the web app and you see the request come in and all the timing and there's any log records in .NET or if anything crashes or like it's all associated with the same request.
- 50:26Now, if we're going to spin up and run a Python function and in Python, you do import logging, you know, logger logging dot info or logging dot error.
- 50:35Like, where does that go?
- 50:36Because you've just logged something in Python.
- 50:39Does it just disappear into the ether?
- 50:41Is it like it goes into the terminal output of the container?
- 50:45It runs it.
- 50:46Who knows?
- 50:46Right?
- 50:47Yeah, exactly.
- 50:47Like it would be kind of useless and not easy to manage.
- 50:51And we're coming at this at the angle, like people write big enterprise apps in dotnet.
- 50:56So like they would expect that they do logging and it's all in the same system.
- 51:00So this demo basically says, OK, one of the API endpoints in on dotnet, it runs a function in Python, which spins up.
- 51:09It uses pandas and as a data frame to look at the weather or in it, like calculate some averages and shifting means and stuff like that to give you back like a prediction for what the weather is going to be like this week.
- 51:20And we just use Seattle was the data set because it's easy because it's always going to rain.
- 51:24And then it's not actually most of the time it doesn't.
- 51:27It's not fair.
- 51:28So it runs that in Python.
- 51:30And in the Python code, we use open telemetry to show the trace of it talking to the database.
- 51:36And we use Python logging and that log information on the trace information all gets put into the same trace.
- 51:42So from .NET from start to finish, you see the request come in on the front end.
- 51:47You see the .NET code executing.
- 51:49You see the Python code.
- 51:50And then if Python crashes or if the log entries go in, they're all in the same environment.
- 51:55So in like an open telemetry dashboard, you see the whole trace across both .NET and Python or in the same environment.
- 52:04And you can just use logging as you would normally.
- 52:06And .NET aspire comes with a like a free open telemetry dashboard that you can run locally.
- 52:13It's like a Docker container.
- 52:15It's really nice to save you kind of spinning up Jaeger or something.
- 52:19Yeah, this is really cool because you've got an ASP.net API.
- 52:23But then when it's called, it uses one of these code gen CSnakes Python integrations.
- 52:28But a lot of stuff just flows through like the Postgres connection string flows through the open telemetry context flows through the logging kind of goes tangentially off.
- 52:40Then it talks to the database in Python and then it returns back.
- 52:43And your example is just pure SQL connections and stuff.
- 52:47But it's like a PG2.
- 52:48But you could use pretty much any ORM or anything, right?
- 52:52There's no real constraint there.
- 52:53Yeah.
- 52:54And it uses a few modules which are tricky like pandas and psycopg is a anyone who's deployed that to production in containers or in different environments.
- 53:05And those is it's hard because like it's like which binary do you use and like importing it is hard.
- 53:11And, you know, you need libpq and like it's a bit of a nightmare.
- 53:16So it's just demonstrate that they're like, this is this is a real thing you can do.
- 53:20And it shows something which is practical in Python and something which is practical in .NET, which is like a proper app with tracing and logging and all the, you know, all those kind of features.
- 53:30And on the Python side, we're using pandas, which, you know, and we're using numpy, which is the C extension.
- 53:36And we're using psycopg, which is like depends on a whole bunch of like third party libraries as well.
- 53:42And stringing it all together and it just looks and feels seamless.
- 53:46Like you see the demo working and you would never know that the weather prediction comes from Python or even that it's running in this way.
- 53:55Like it just looks like and it's implemented like it's part of the same API.
- 53:59It's call a function and one of the libraries and off it goes.
- 54:02But yeah, it's all connected together like this.
- 54:04All right. I'll let either.
- 54:05We're about out of time, but I'll let either of you take this.
- 54:08That was a web example.
- 54:10What about something like WPF or Maui or any other desktop app possible?
- 54:17Yeah. So because I'll talk about Maui because obviously that's the more modern UI framework.
- 54:24Give a really, really quick definition to listeners who probably think of that as part of Hawaii.
- 54:29It's kind of the next evolution of Xamarin, which people may be familiar with the way that you do cross-platform UIs.
- 54:38I'm definitely not the expert on Maui, but the idea is that, you know, you can have something that is a bit more portable from a application experience,
- 54:46if you want to target like a mobile application, like an Android or an iOS or run as a desktop application on Windows or something like that.
- 54:53So just like a level of portability and some structure between UI concepts.
- 54:58But fundamentally that's just running .NET behind the scenes.
- 55:01So there's no reason why we couldn't also drop in a, a bit of a sea snakes in there and have that, yeah, work with some kind of backend service.
- 55:11Like you could use this and this is something that Anthony and I have talked about trying to, trying to get a demo of doing like a local model running with,
- 55:18like, so you, you pull a model off of hugging face or something like that, just runs it locally to do some kind of AI inference or whatever the case may be for the scenario you want to tackle.
- 55:29But that way you don't have to be, you know, calling out to an external service.
- 55:33Now, while sure running like a local model, it's doable with .NET.
- 55:38It is just way easier to do that with Python.
- 55:41And all the examples you would find are going to be written in Python first and foremost.
- 55:46So trying to then go, okay, well, so this is doable in Python.
- 55:49So what's the C# equivalent?
- 55:51How do I find the equivalent .NET libraries on you yet?
- 55:54And so on and so forth.
- 55:56Well, you know, like with sea snakes, you could expose that model, create some surface areas in just Python APIs that is then going to be consumable by a .NET application.
- 56:08So yeah, it's something you can definitely do not 100% sure whether you can do it on mobile.
- 56:13I think that's probably stretching it a bit far just because of the way that we have to bootstrap Python with inside of it.
- 56:19And you'd probably be limited from a mobile OS runtime to be able to do that kind of bootstrapping.
- 56:25But at the very least, like a desktop application, or if you wanted to tackle with like a cross platform UI framework like Avalonia.
- 56:32So Avalonia is a cross platform .NET UI framework that means you could build for Linux and Windows and macOS at the same time.
- 56:41You could then run sea snakes under the hood of that to then have that desktop experience that is leveraging Python for where Python is going to be better suited to tackle a problem.
- 56:51.NET for a different set of problems that's better suited for that just inside this application where you would have no idea as the end user that you're like that that's what's actually happening.
- 57:00Very compelling. Very compelling.
- 57:02Very compelling.
- 57:03All right, Anthony, let's close this out with one final thing.
- 57:05I think this is important and we haven't really touched on it yet.
- 57:07You guys started the conversation by saying this is really important for data science, right?
- 57:11Data science use cases.
- 57:12And I made the joke about copying a million items to add one to the list.
- 57:15But what about sharing data like NumPy across different, you know, the .NET side and the Python side and so on?
- 57:24We have tackled that specifically.
- 57:26So there's something called the buffer protocol.
- 57:28It's a PEP from a few years ago.
- 57:31It's a kind of an API for the internals of something like an ND array in NumPy.
- 57:39And the buffer protocol with it, we support that.
- 57:44So if your function returns a buffer, which is a type, then we can access the internals of that buffer directly without having to convert the data.
- 57:54What this means in reality is that if your function returns a like a NumPy array, like an ND array and the internal values in the ND array are a specific type like a 32 bit integer, you can access them as 32 bit integers in .NET without having to marshal or copy data.
- 58:13That's awesome.
- 58:14You reach inside of the ND array, just grab its actual memory and just say, got it. Let's go.
- 58:20Got it. Let's go.
- 58:21Yeah, that's exactly what we do.
- 58:22And so that's a is in .NET.
- 58:24It's called a read only span, which is the you can write the data as well.
- 58:28So you can do it and you can actually just modify the values inside the NumPy array directly from .NET without having to copy backwards and forwards.
- 58:36And we support pretty much every like low level types that NumPy supports.
- 58:42And that means you don't have to do a .2 list on the NumPy array because like that is really expensive.
- 58:49And also .NET works in a similar low level numeric types that NumPy does.
- 58:54You know, it will work with 32 bit ints and 32 or 64 bit floats, for example.
- 59:00So like if you're storing a 2D matrices in 32 bit floats in NumPy and then you want to send that to to .NET to I don't know, like pick out some specific values or like do some calculations in .NET, then you can just chuck that back as a buffer.
- 59:17And there's information in the docs about how to do this and then you can just access the values directly out of memory.
- 59:23So it's super fast.
- 59:25And if you even want to like fill a buffer with values from .NET, then you can do that using the writable span.
- 59:32That's pretty awesome.
- 59:33So, yeah, that really is leaning into the data science benefits here as well, isn't it?
- 59:37Nothing ever goes wrong if you're sharing memory across runtimes for different programming languages.
- 59:42It's no, I don't see how this could go wrong.
- 59:44Yeah, perfectly fine.
- 59:46And that's why I pushed to make sure we supported read only span.
- 59:49Yeah, right.
- 59:50Okay.
- 59:51No, I think it's great.
- 59:52I think it's great.
- 59:53And the UI stuff with Avalonia is something I'm going to look at.
- 59:57That sounds really cool to play with.
- 59:58So, all right.
- 59:59Final thoughts, you guys.
- 1:00:00People out there going, oh my gosh, I cannot believe we can finally do this with our .NET app and plug in some of the other things we're already doing in Python.
- 1:00:08Should they?
- 1:00:09Is it ready?
- 1:00:10If somebody were to take this and build a thing, is that a good idea or should they wait?
- 1:00:13Yep.
- 1:00:14They should download it, give it a go.
- 1:00:16The only thing we're possibly changing at the moment is the API based on feedback.
- 1:00:20We're trying to keep that as stable as possible right now.
- 1:00:22So we're only making changes that are backward compatible.
- 1:00:25And yeah, we just want to see more use cases and people can get more feedback.
- 1:00:30So yeah, please pull it, grab the samples, spin them up, build something, tell us what you made, and then give us any feedback.
- 1:00:36Well, congrats.
- 1:00:37It looks really, really nice.
- 1:00:38Definitely, definitely looks promising.
- 1:00:40And I think it's, it solves a lot of the problems that the other attempts had.
- 1:00:43So nice work.
- 1:00:44Thanks, Mike.
- 1:00:44Thanks.
- 1:00:46Yep.
- 1:00:47Bye guys.
- 1:00:52Be sure to check out what they're offering.
- 1:00:53It really helps support the show.
- 1:00:55This episode is sponsored by Posit Connect
- 1:00:58from the makers of Shiny.
- 1:00:59Publish, share, and deploy all of your data projects
- 1:01:02that you're creating using Python.
- 1:01:04Streamlit, Dash, Shiny, Bokeh, FastAPI, Flask, Quarto,
- 1:01:08Reports, Dashboards, and APIs.
- 1:01:10Posit Connect supports all of them.
- 1:01:12Try Posit Connect for free
- 1:01:14by going to talkpython.fm/posit, P-O-S-I-T.
- 1:01:18And this episode is brought to you by Bluehost.
- 1:01:22Do you need a website fast?
- 1:01:23Get Bluehost.
- 1:01:24Their AI builds your WordPress site in minutes
- 1:01:26and their built-in tools optimize your growth.
- 1:01:29Don't wait.
- 1:01:30Visit talkpython.fm/bluehost to get started.
- 1:01:34Want to level up your Python?
- 1:01:36We have one of the largest catalogs
- 1:01:38of Python video courses over at Talk Python.
- 1:01:40Our content ranges from true beginners
- 1:01:42to deeply advanced topics like memory and async.
- 1:01:45And best of all, there's not a subscription in sight.
- 1:01:48Check it out for yourself at training.talkpython.fm.
- 1:01:51Be sure to subscribe to the show,
- 1:01:53open your favorite podcast app,
- 1:01:54and search for Python.
- 1:01:55We should be right at the top.
- 1:01:57You can also find the iTunes feed at /itunes,
- 1:02:00the Google Play feed at /play,
- 1:02:02and the direct RSS feed at /rss on talkpython.fm.
- 1:02:06We're live streaming most of our recordings these days.
- 1:02:09If you want to be part of the show
- 1:02:10and have your comments featured on the air,
- 1:02:12be sure to subscribe to our YouTube channel
- 1:02:14at talkpython.fm/youtube.
- 1:02:17This is your host, Michael Kennedy.
- 1:02:19Thanks so much for listening.
- 1:02:20I really appreciate it.
- 1:02:21Now get out there and write some Python code.
- 1:02:23Thank you.
- 1:02:31Thank you.