Reloadr – Hot code reloading tool for Python
github.com
github.com
The in-memory reloading (which may be seen at: https://github.com/fabioz/PyDev.Debugger/blob/main/_pydevd_b...) unfortunately doesn't work all the time (fixing the references at runtime in Python is difficult), but it works well enough for the cases where you have a simple function and change it (that file does explain some of the issues that the reloading faces).
One major issue is that after doing the reload, you need to get out of the function and then back into it to see the difference -- so, if you're stopped in the function in a breakpoint, it's not possible to simply drop the function and restart it as Python itself doesn't support that... maybe that's a good idea for a PEP ;)
Anyways, when it works, it's pretty nice in that you don't have to restart your whole debug session to see changes (the other approach, which webservers use in general is just restart the whole process, so, for instance, Django has an initial process just monitoring for changes and a 2nd process which is actually serving requests which is restarted when something changes -- not as good if you have a heavy startup or are working with something where you have to click through multiple things to get to the state which you want).
(https://news.ycombinator.com/item?id=26034851)
I wonder if this is used seriously by someone.
I always thought that this has a potential to greatly improve the development productivity.
I remember I saw live coding videos from Notch (Minecraft) on some gamejam where he intensively used hot code reloading for Java. About 90% of his development time, he had the game running, and tuned the logic of some game elements live, and immediately saw the result.
I know that Erlang also has such a feature, such that you don't need to shut down your production system, and that you can update it live while it keeps running.
There's still some bugs I have to iron out, though, it should be better in a month or so. Still, you can pip install it if you want.
The author posted here: https://news.ycombinator.com/item?id=26039366
thanks
While these things only take maybe a few seconds each, thats a few seconds that you’ll perform maybe hundreds of times. Reducing context switches is always a good thing.
When working on a video game, restarting the game every time you are tweaking the behaviour of an entity takes a lot of time. Hot code reloading while keeping the state of the game makes development much easier.
On the other hand, when the entire state is stored outside of the Python app (eg: a web app using a database), restarting the entire process and reloading the page is fast enough and more reliable.
Visual Studio seems to have "edit and continue" which I only knew from VB, but seems to be available in C# and C++ too. Not the same, but it shortens the development cycle in a similar fashion.
The feature in Python reminds me on hot code loading in Erlang which I found quite interesting when i first read (never used) about the feature. I wondered which other languages provides (or didn't) an equivalent solution. Back then it felt a bit scary, with not much more than RCS as VCS around :) But I think Erlang implemented it for increased up-time, not for increased development cycle?
To keep the up-time high, today even parts or entire operating systems are patched 'live' (ksplice, kpatch, kgraft, AIX live update) (https://ksplice.oracle.com/) (https://www.ibm.com/support/knowledgecenter/ssw_aix_72/insta...). Here too to prevent downtime and technologically completely different from what is presented here.
To be fair, this was a few years ago now, maybe worth another look?
If only more environments had similar tools. Once you've worked with this other tools feel rather primitive.
from importlib import reload as r
Then in the ipython shell you can type r(module) to reload the entire thing.So I'll often have the editor in one terminal window, and ipython in the other one, save the code and switch back the repl, and type r(t)
import mymodule as t
Now tie in some ipdb [1] and you have a pretty sweet setup. Also depending on your serving framework it might also have hot reloading as well, tornado, flask, etc.[1] https://github.com/gotcha/ipdb
[2] https://www.tornadoweb.org/en/stable/autoreload.html https://werkzeug.palletsprojects.com/en/0.14.x/serving/
It's not about simply saving time doing it manually. It fundamentally changes my dev-test/experiment workflow.
I love being able to tab to a server file, make a change, tab to a client file, make another change, and instantly test it in a browser.
I jest, but really I feel like we've made things so complicated it just gets in our way. Our Javascript pipeline is huge, complicated, and requires you to sit down and wrap your head around it every time you want to modify it.
If I get to greenfield another project, it's going to be so bloody simple.
They don't resemble traditional websites/CRUD apps a whole lot. Much closer to Sim City + Factorio.
When it comes to making your standard issue website/webapp, I think some of them could stand to be simpler.
It's always simple when it starts up. It's after months/years of iterating without having a decent battle plan that it becomes complicated.
It is a bit like the unix philosophy of "simple apps, in sequence" -- in the sense that it clarifies what development should be.
PHP apps, at their best, were multiple "run once and die" endpoints -- the drive to "Front Controller" and "run long" apps throws away much of the value of this paradigm.
Part of the community problem, id say, is that this paradigm was never well-articulated; and always associated with "a mess".
People like rasmus tried: yes have your long-running apps (eg., in Java), but your front-end should be die-quickly.
Another way of saying PHP has to bootstrap everything on each request (which is a bit of an overreach; it doesn't reload C libraries, and you can do opcode caching and data caching, etc), is that PHP throws away everything you've done after each request.
That means everything that you build that isn't output somewhere is garbage.
If you spend a lot of time making a sculpture out of garbage, only to throw it away, you'll have poor performance. If you use the minimum of scaffolding to send out the bytes you want to send, you can get pages (or api responses, whatever) with a few ms of overhead in addition to your underlying data fetches. You could certainly go faster with something else, but PHP can be decently fast.
On the other hand, your code won't look at all like what people think of as 'modern' PHP.
1. slack backend is written in php and other big companies. 2. php is actually faster than python/ruby 3. opcache in production (which is both simple to activate AND a well-known best practices among people doing php professionally) eliminate the performance hit you will get. 4. your backend language performance is often far from being the bottleneck in most endpoint where performance do matters , it's often either a database optimization problem, an algorithm problem or a really CPU intensive tasks that you will offload to a dedicated library written in C.
I perfectly understand PHP does have a lot of flaws, but production performance is not one.
* backend in php8 with symfony framework
* postgres
* frontend is only html+tailwind css and 2 lines of javascript here and there for a smooth transition effect (https://christopheraue.net/design/fading-pages-on-load-and-unload)
and I just got my PWA accepted by google store , and my 18 lines react-native app embedding the website in a page view for apple store is in review.no javascript madness, and the app is so snappy that everybody ask me which kind of react optimization i'm using : "the one where you remove it completly"
It's an improvement on some previous attempts at the solution that would crash when reloading invalid code. Hupper does not have this problem due to its multi process design.
[1] https://docs.pylonsproject.org/projects/hupper/en/latest/
I was always marveled at the capacities of xDebug, and never stopped using it since I found it (Some 10+ years), and honestly, coupling it with PHPStorm is another level.
But still, hitting reload on the browser, or running a script from the CLI gets tiring after the 20th time; so I made this little class where you would call a method, and the method will reload the script where the code was located continuously until I managed to get it to do what I wanted.
It was very Johnny Mnemonic, but it worked.
%load_ext autoreload
%autoreload 2
in ~/.ipython/profile_default/startup/autoreload.ipy%load_ext autoreload %autoreload 2
now every run automatically reload your modules.
(just kidding)
It's great to see I am not the only one interested in this way of coding. I will have a look at all the alternatives your posted about, let's get hot code reloading in Python awesome!
Join the conversation on FOSDEM's Matrix channel tomorrow to discuss the subject further: #python-python_reloading:fosdem.org
The examples just have loops where the reloaded function is called in a loop. Is this just updating the definition of the function between calls?
It's pretty darn neat. I'm going to play around with it a little more over the weekend.
For production usage, maybe that wouldn't be considered safe, but there are companies that provide services with this purpose (e.g. JRebel).
Say you have to load a lot of data, but once loaded you can trigger the issue by just hitting a button.
With hot code reloading you could incrementally add debugging code and try your fix, without having to restart the program and wait for the data to reload each time.
It’s less about saving the time to re-run something, and more about removing conceptual overhead (I think).
I cobbled together something similar a few years back using the reload stuff from ipython and a script I think GuidoVR wrote that I stumbled on. I like to debug on the cli (pdb++), I made it autodetect changed modules and reload those whenever i entered the 'reload' command. For me having to decorate things first would be a no-no.
My helper worked really nicely for some projects but broke with others. Well done on getting something out there.
In general, though, it's going to be hard.
The idea is to use virtual functions, and recompile new classes into a shared library. The shared lib is linked into the running program, and old instances of the classes are deleted and replaced by new instances constructed from the shared library.
I am working on a library called hscpp using the same (stolen) idea here:
https://github.com/jheruty/hscpp
Proof of concept demo:
https://m.youtube.com/watch?v=pjGngeKgni8
hscpp is still very alpha, and I’m sure I’ll find lots of bugs as I work on a “real” demo. In contrast, Runtime Compiled C++ is quite mature and is used in real game projects.
Note that this approach very much limits your architecture. For example, you won’t be able to use statics, as the newly compiled shared libraries won’t see them.
It’s a finicky thing, worth it to me, but not something you can just plop in to an established project.
While investigating the topic I found a lot of projects: - https://github.com/yiblet/inquest - https://github.com/mpolney/hotpatch - https://pypi.org/project/patchy/ - https://pypi.org/project/plone.reload/ - http://svn.python.org/projects/sandbox/trunk/xreload/ - https://pypi.org/project/hot_reload/ - https://pypi.org/project/Reloadr/ - https://pypi.org/project/hotswap/ - https://pypi.org/project/reloading/ - https://pypi.org/project/hotreload/ - https://pypi.org/project/livereload/ - https://pypi.org/project/reimport/ - https://pypi.org/project/livecoding/ - https://pypi.org/project/pylivecoding/ - https://pypi.org/project/nameko-hot-reload/ - https://github.com/albertz/PythonHotswap - https://github.com/mikr/pyqtboiler/blob/master/baseapp/utils... - https://github.com/narfdotpl/hottie with decorator - https://github.com/ipython/ipython/blob/f8c9ea7db42d9830f163... - https://github.com/breuleux/jurigged
I thing that xreload from Guido and the code inside ipython both provide the best hot-patching. But both projects miss the feature to watch for changed files.