 Okay, welcome back. I really like the ads. It's like we have the Python TV or something like that So I hope everyone is enjoying the conference I'm here to introduce Gina or Gina and she will teach me how to produce that. I think you were I was trying to learn that in some rehearsal and a fail So, yeah, it's welcome. Hi. Thank you. Thank you for having me. Yeah, it's Gina actually It's an Italian name even though I'm German. So Yeah I can I can do a bit better. Cool. No problem So, what I was doing from Germany is Germany. Yeah, so I'm about I don't know maybe 10 minutes from Frankfurt I'm mine here. So quite central in Germany and Yeah, it's a hot day. It's a warm day. I went rock climbing this morning. So I'm I'm perfect right now Right. That's super nice. I have a super important question for you. Is it space or tops? I I I do Understand the ideas behind pep 8 and so I have switched my code to spaces But personally I've always been a huge fan of tabs and actually tabs for indentation and spaces for alignment That has always been my mantra, but I am playing by pep 8's rules now So, yeah Nice, I really like now a lot of people will complain to you in the chat. So sorry about that Okay, so Gina is going to be talking about driving 3d printers with Python and Yeah shows she's a passionate called monkey gamer hobby biker and creator of a maintainer of auto print I think she's going to be talking about that So I'm not going to steal more time for you and Thank you very much for being for being here in our Python presenting And I hope we see each other next year in down the end. So yeah, the status is yours. Good luck Thank you. Okay. So yeah, as I was already introduced my name is Gina Hoiske and I'm talking to you today about Driving 3d printers with Python and more specifically the lessons that I learned from that Some words about myself first Gina Hoiske. The last name is actually usually Making more people pause than the first one. I'm a software engineer. I'm a maker. I'm a hobby baker 100% nerd and I'm also in the happy position to be a full-time crowd-funded Open-source software developer and I'm the creator and maintainer of octoprint, which is also the project that I am working full-time on fully crowd-funded What is octoprint? Well, it's the snappy web interface for you 3d printer. It's open source licensed under a GPL v3 Has currently something about 100,000 confirmed users unknown actual number because the tracking is opt-in and GDPR conformant or that so I only have a rough estimate there The back end of this Application is completely written in Python the front end is your usual mix of hdml css and java script and you can find All out about this about this project on octoprint.org Octoprint is as I already said an application so something that users install themselves and the target audience for octoprint are Yeah, pretty much owners of consumers 3d printers. So the things that you can buy in your hardware in the hardware store these days actually Usually people who are makers and tinkerers who maybe want to print stuff for their miniature wargames or terrain pieces or something like that Or who are into model plane flying and want to print parts for that or who are just into 3d printing as a hobby There are also some businesses here and there So we are talking about end users more or less with some small exceptions And these end users are rarely developers themselves So we usually look at people who just take on a new hobby in the shape of a 3d printer Or who acquire a 3d printer for their business and they now want to drive it somehow want to monitor it somehow And this is where octoprint comes in Additionally octoprint targets as it is the platform that it runs on primarily targets small single PCB Linux computers Like for example the raspberry pi that you might have heard about so small cheap 35 to $50 and Easily just installed next to the printer with octoprint on it and then you are ready to go Important is that while octoprint primarily targets this platform. It still is platform agnostic agnostic So if you still have a laptop gathering dust you can also if you desperately want to throw some windows on there and run octoprint from that or if you have an old MacBook that also works and this specific kind of target audience and platform restrictions and also the whole Thing that octoprint is an application installed by end users and run by end users and in a way Then also maintained by end users makes for some unique challenges or at least I found them to be pretty unique Because the literature is full about challenges for library development But not so much about challenges for actual end user-facing applications And I want to introduce you to three of them that I encountered over the past almost nine years now of doing this project challenge number one is Initial installation and keeping things up to date So I already said that octoprint is installed on premise by end users So they download something and then have to install it themselves Usually when you have some Python application Then you will just tell people that they just need to run pip install octoprint preferably also bootstrap a Python environment in the first place and Then they are good, but this doesn't work with the end user with the audience that we are talking about here What octoprint instead does is that there is a pre-configured Raspberry Pi image that Contains octoprint contains all the dependencies contains the whole Python environment already set up with the virtual environment with all dependencies installed with a C compiler installed for Compiling native extensions and all that and users can just download that image flash it to an SD card and Then boot it up and octoprint will then take it from there through a wizard and help them get started So that has worked out really nice Question of course is what about if you want to install it on something that is not a Raspberry Pi? Well, as I said, this is not the primary platform So there be some dragons, but it is in principle supported There is a huge variety of encountered environments out there So I've had people who have installed octoprint on free BSD I've had people install octoprint on some old open WRT router that they still had a Lying around things like this so that it can be very diverse and can Yeah post some unique challenges actually with regards to dependency L and all that and that means that the code in Octoprint needs to anticipate some of these things. So for example the IPv6 dual stack on Windows works entirely differently between compared to Linux from a Python point of view and File locking works differently. For example, I cannot just I do have to keep things in mind when I do something like log roll over or things like that in my client code because Yeah, things are just not working as easily under Windows because you cannot write a file that is currently in use So these are things that have you that you have to keep in mind there and Then you now have installed it. What do you do now? How do you keep things up to date? Again, the usual approach here is to tell people well, just run pip install. There's you octoprint and that will take care of that But again, this is not really going to work out with the target audience here because they Usually they don't even want to touch the command line that alone one any kind of commands in there so what I how I solved this in octoprint is that Octoprint as well as any kind of plugins that are installed into octoprint more and plugins a bit later Are there there isn't there's an update mechanism for those built into octoprint Which itself is a plug-in by the way But that basically takes care of this pip installed as you in the background Gives everything a nice little UI people get update notifications when something new is out and they just have to click a button And everything will be taken care of them making updating a very Easy and straightforward appearance So that is octoprint. What about stuff like pip? You might have seen this message there when running pip install or pip update or whatnot You are using pip version x. However version y is available It turns out that this is a very scary message for end users because they see a message that looks like something is wrong something is broken and it tells them something cryptic that they should be doing and They don't really know how to do that and yeah That that has caused a lot of support overhead in the end The the solution to this was actually to hide the message in the update dialogue and instead Off an option to update pip separately, but only for power users so to speak and The underlying operating system Octoprint does not take care of that Some users do update it some don't and the result is that we sadly also once again have to face a somewhat mixed environment Sometimes broken environment. It's hard to test. There are some additional support overheads caused by this and a lot of people are still of the Of the conviction that never touch a running system is the mantra to go with these days and I Obviously do not agree the only way to get people to actually install updates However, at least in my experience seems to be to tell them here's a new feature. Here's a bug fix so That seems to work with Motivating people to touch a running system and actually install updates in a very timely matter after each release so Lessons learned here is really try to control the runtime environment, but Stay flexible enough so that you don't pretty much put people into a prison that you created Make updates very easy so that the that people will actually apply them and Also use features as a motivator for touching a running system and for getting people to apply the easy to apply Updates in the first place in short really just enable them to keep their systems updated challenge number two maintaining backwards compatibility for a whole ecosystem So I already dropped the word plug in here and octoprint has now Sported a plug-in system since 2015 since version one two one point two point oh and we now are looking at over 300 plug-ins registered on the official plug-in repository plug-ins do stuff like you could get an email notification when the print is done or Fancy time lapses that make the print like look like it is growing out of the print bed work arounds for weird firmwares from from 3d printer from cheap 3d printers And stuff like that so we haven't really established plug-in ecosystem here now and That means that suddenly octoprint is not just a little application that I just write in in in my home office here But it is pretty much become sort of a platform and the platform requires stability why well if I constantly change everything and throw APIs away and and fire up new ones and make backwards incompatible changes all the time and I don't think that plug-in authors would be as motivated to actually develop against this platform as they are now Which means I don't have an ecosystem anymore, which means the users that are drawn to the ecosystem Partially even more so than the convenience that octoprint alone offers them. Yeah, these users are now somewhat unhappy and disgruntled So what does this mean? Yeah, I actually have to spend a significant amount of time in octoprint's update cycle on Yeah, pretty much going through dependency updates that are that I do need to need do need to do and check them for breaking changes because it actually turns out that yeah, a Lot of third-party dependencies regularly will break your code even with patch updates and the reason here is that even if I adapt my code for for dependency changes for for backwards incompatible changes on third-party dependencies if any kind of plug-in now also uses the same version and Does not anticipate this change then stuff might break Unintentionally on site of the user again disgruntled user unhappy user. We don't want unhappy users So what I usually do here is when some kind of backwards incompatible change in an in an updated dependency rushes in I Look if I can somehow rep it so that it becomes transparent for everyone using this code Of course, it means a lot of work. It means a lot of version pinning It means a lot of reading through changelogs and sometimes even commit histories, but it means also that there is a lot of stability on the platform Another example where additional Yeah, work and overhead is needed is a code refactoring if I spot in typo in my API I cannot just fix that because someone might already be using this API I have to think long and hard about any kind of changes in calling parameters or in return values or even just in side effects of certain commands and that adds up so Usually that also means wrappers Internally that make sure that everything every changes had our handle transparently for any kind of calling code and Finally, the biggest the biggest challenge actually over the past years has been the whole Python 2 Versus Python 3 migration situation So as we know Python 2 has been end-of-life since January 1st 2020 But octoprint for the longest time actually only supported Python 2 because I will admit That was a big mistake of mine when I created this back in late 2012 I simply did never anticipate it to become a thing and Then it became such a big thing that I never found the time to actually do the migration in a timely manner. So Yeah, things took a long while and now I also had to worry about the whole ecosystem and this was pretty much my whole late 2019 and 20 and full on 2020 that I'm going to talk about now, which is migrating an ecosystem to Python 3 So how I tackle this with octoprint was I mean it was absolutely clear that I had to make the cut at some point to at least make octoprint compatible to Python 3 for now it also still supports Python 2 but also Try to push the whole community the Python authors at the plug-in authors and the and the users themselves over to Python 3 and That needed a lot of preparation Apart from just rewriting parts of octoprint or migrating parts of octoprint And the approach here was first of all a long preparation and briefing period So while I was still working on the migration of octoprint itself I Also worked with the community so that we could Create some kind of migration guide So I had some plug-in authors work against the new version work against Python 3 figure out the common pitfalls that they encountered during this and Then tell me about it and document all of that in a long document on on the on the on the page and I also threw python compatibility information in plug-ins and the plug-in repository so that octoprint would be able to look at a plug-ins code and Know whether it could even load that under Python 3 or not So as to not cause any nasty surprises for users who Would just try to load a plug-in that is Python 2 compatible only under Python 3 I Rolled out where early preview versions of Python 3 compatible octoprint for everyone Well primarily plug-in authors But also end users to test against and see if everything was still working as expected and if not fix it in time and make it compatible to both and I mentioned the plug-in repository already. We also switched that to requiring newly registered plugins to and support at the very least Python 2 and 3 starting in late 2019 if A plug-in author decided they only wanted to support Python 3 that was of course also fine But Python 2 only plug-ins were banned from a certain point then just before the first lockdown in Germany actually I Released octoprint 140 that Supported Python 2 and 3 on March 4th 2020 the release went very smooth The only problem was that some users were confused because they thought Octoprint was now only supporting Python 3 but that was a communication issue that thankfully was quickly handled with a FAQ entry and Since then we've been more or less in ongoing migration mode. So Users are asked to report any kind of incompatibilities with plug-ins that they discover with Octoprint running under Python 3 to the respective plug-in authors so that they can migrate them and make them compatible We also the community or rather someone from the community also created a migration script that allows old Image installs that still ship with the Python 2 virtual environment to be swapped over to a Python 3 image With yeah, pretty much next to no input needed from the or action needed from the from the end users So all they had to do was just enter one command Into the command line the scary one and then everything got migrated their plug-ins got migrated and all that and that was really well accepted and Also, we are the the community is now doing some motivation to get people to move over to Python 3 with their existing instances by first of all we discovered that octoprint is Performing way way better under Python 3 and also some plug-in authors decided enough is enough and made their plug-ins Python 3 only and that has also worked as a nice motivator and Because it's always nice to look at some fancy graphs when saying something like this I've brought this so this is the compatibility Python 3 compatibility of octoprint plug-ins that are registered in the octoprint plug-in repository Sadly, I only have the data starting in May of 2020 and the yellow line is the amount of Plug-ins that are Python 3 compatible and you see that pretty much right after After the launch the release of octoprint one for all with Python 3 compatibility in March We were still somewhere around 33% or something like that But there was a lot of growth because plug-in authors saw oh there suddenly a Python 3 compatible octoprint version We need to get stuff going now and then I also discovered that the compatibility Information for some plug-ins in the plug-in repository was actually outdated that was the first big jump there The second big jump is the Python 3 migration script in September that I mentioned Then there was also the release of the new Python the new octopi image with Python 3 on board and all of that has led to a steady growth and we are now at something like 80% Python 3 compatibility in the ecosystem. I Guess the rest The the the leftover 20% are probably abandoned by this point So once octoprint switches to full Python 3 compatible only Python 3 only compatibility Wheels we'll know for good. Some of them will probably be adopted and those who are not going to be adopted Well, might not have been as interesting Okay, so lessons learned here is Rechange logs of dependencies if you can find them and use version pinning to your advantage You really do not want a third-party dependency to make stuff stop working for your end user somewhere In the field which then do absolutely not know what to do here Be prepared to work around breaking changes rappers and sometimes even monkey patch patching can be a friend here And most importantly of all is really just keep the ecosystem worth living in even with this huge migration with Python 2 to Python 3 We still have a lot of Motivation by plug-in authors to work in this system and we still get users that are like well This plug-in system is amazing and octoprint is amazing So apparently this has worked And finally the last challenge that I want to talk about briefly is performance. So We have one Python process here that has to serve a web interface and several clients connecting to it also including over a web socket Keeping a printer running so constantly feeding a data then plug-ins doing all of this as well So this is a ton of stuff that is going on and all of this is locked to one thread at a time Thanks to the global interpreter lock If I had known about the global interpreter lock back in 2012 and I started this as a pretty much Python newbie I would probably have chosen a different architecture. I mean not Python, but a different architecture internally, but as things are now I'm stuck with it and the problem here is that a lot of Octoprints tasks are not IO bound, but they are CPU bound So checksums needs to be need to be calculated or data needs to be processed stuff like that So it is not as easy to just say we can throw it into some other place and Or just use as ink and everything will work fine And thanks to data sharing with plug-ins or rather between core and plug-ins being necessary Yeah, that also makes multiple processes a bit difficult because now you also need to look at how you do actual data sharing and And pretty much inter process communication and things like this And this is actually something that I still have not figured out and as I said if I had known about the gill a bit for a Bit sooner or back in late 2012 as someone coming from the Java world. I would probably Kept that in mind and designed accordingly, but now I have to somehow work around it and this is still ongoing and Yes, I said finally the underlying platform the Raspberry Pi or other single port PCBs I usually somewhat limited in resources modern pies have like four cores and Up to eight gigabytes of RAM so it's become better, but still it's something where you really need to keep Yeah Every every single change with every single change you have to keep in mind how it might impact performance at this point So I'm not anymore in the Comfortable position that I can just say I'll just throw in this feature and everything will work as before because features Need additional resources and that always means I have to think hard if I really want to do that Yeah lessons learned here Look that I yeah I made good experience with trying to actually extract CPU bone tasks as much as possible into their own processes so stuff like File analysis of the stuff that is supposed to be sent to the printer to look how big it is going to be and how much Filament it is going to use and stuff. This is these are things that you can really nicely extract into their own processes and In general be very conservative with resources because even small changes can mean that it stops working efficiently for someone and now their prints are no longer going through smoothly and Yeah, this is more like a to-do for me than a lesson learned explore IPC options and also maybe native bindings here and here and there So I actually want to take a closer look at site and for that matter and see if it can maybe help me in making some stuff a bit more performant especially in the communication layer with the printer and And That already was my show. I want to thank you for your attention and for being here also, I want to thank the organizers for allowing me to be here and Yeah, if you want to get in touch or something my Twitter handle is futile you can also find me on github under that and pretty much everywhere else and Yeah, my website is also futile.net and as I said anything about octoprint you can find on octoprint.org Thank you Okay, thank you, Gina. That was that was really nice super interesting We have a few minutes and we have some questions. So I'm going to start asking those So maybe we can reply you can pray to all of them So the first one is people is asking if you are running the project alone and they're saying yeah, it seems like a lot of work So they want to know if you have a people contributing. Maybe they want to join I don't have a small team. So I'm the only one who works on it full-time I have some regular contributors I also have some very enthusiastic plug-in authors and people who help me with all of the community management all around But if you look at the commit history, I think it's pretty much still something like 90% or so that is mine Or maybe more. I don't know. I actually have not run stats on it to be honest But yeah, a lot of people think I'm at least 12 Man usually because oh my god a woman with an open source project that cannot possibly be But no, it's really primarily just me Okay, so good time for everyone to take a look to your github and help you So the second question is were you able to reach the plug-in authors to rework the plugins for Python 3? And how long was the process of migrating the most plugins? so The thing is that I do regular devlogs for the community called octoprandon error And this is also where all of this obviously prominently featured and a lot of plug-in authors are watching these So that was one one way to reach at least the majority of them A lot of them are also active on our forums and on our discord and that Also helped in in getting the word out so to speak and the rest then was actually done by the end-users so we had one plug-in author actually write a little plug-in that would flag all the plugins in someone's installation that were not Python 3 compatible and Then it even had a little button to open a github Tickets directly which turned out to be not that good of an idea because suddenly some plug-in authors were one swamped with 20 identical tickets that pretty much said please update to Python 3 So that had to be changed again, but it worked nicely. So the the the crowds Showed that it wants to keep using these plug-ins and and actually held there Yeah, and the migration seems to have been quite painless for most of course some things are a bit tricky So for example, if you do a lot if for some reason you did a lot of String processing somewhere and did not really take care of the whole bytes versus a unicode situation then that was quite painful and yeah, I I Would have to look into the migration guide now But there were some other pitfalls that really people made made people stumble myself included But that's thankfully now documented. So yeah, okay, so I think we have time for the last one Maybe you were playing to that way In your in your slides, but they were asking if using multiprocessing share memory will help with your problem Maybe the problem is that the way that octoprint itself gets started up has so far cost me a lot of gray hair With with using multiprocessing So I actually have not yet been able to get it to work without something breaking Absolutely spectacularly. Uh, so I'm actually looking into an alternative but yeah cool Okay, so we are just in time. So thank you very much for being presenting here It's super important to have a you or the next speakers In this conference. Otherwise, it's going to be super boring And I hope I see you next year again And for everyone else, uh, if you want to continue the discussion or ask questions You can go to wonder me and talk Using your cameras or you can find Gina on the matrix chat So thank you very much Bye. Bye. Thank you