7 ms·
As other have pointed out, the best way is to restart the process. If you are looking for hot reloading you should look at programming languages that run in BEA
by brodul 6y ago
As other have pointed out, the best way is to restart the process. If you are looking for hot reloading you should look at programming languages that run in BEAM like Erlang and Elixir[1].
There are some gotchas with hot swapping, but it's designed and used in production for many years [2].
If somebody knows other languages/systems that do let me know.
1. https://elixir-lang.org/ https://elixir-lang.org/
2. https://stackoverflow.com/questions/37368376/how-does-erlang-hot-code-swapping-work-in-the-middle-of-activity https://stackoverflow.com/questions/37368376/how-does-erlang...
- dig1 6y ago> If somebody knows other languages/systems that do let me know. Clojure can do this via namespace live reload. ClojureScript is already doing this inside figwheel [1] environment. CommonLisp had this since dawn of time. Kawa [2] can do it, but because it aggressively optimize the code, you need to be careful. Racket can do it as well [3]. [1] https://github.com/bhauman/lein-figwheel https://github.com/bhauman/lein-figwheel [2] https://www.gnu.org/software/kawa/ https://www.gnu.org/software/kawa/ [3] https://github.com/tonyg/racket-reloadable/tree/master#readme https://github.com/tonyg/racket-reloadable/tree/master#readm... EDIT: added Kawa and Racket.
- dmux 6y agoTcl as well.
- em-bee 6y agoi believe smalltalk can do this as well. there are also lesser known languages such as pike. although pike doesn't do hotswapping of actual objects but only allows the hotswapping of the compiled classes used to instantiate new objects. this works well enough on webservers where new objects are created for every request, and only live as long as the request is processed. there is a platform called open-steam that emulates hotswapping by using proxy objects. when i reference an object, i actually get a proxy, that then references the real object. when i update the code, then the proxy gets updated to the new real object, and for me it looks like the object i am referencing has been hotswapped.
- rrdharan 6y agoJava with DCEVM or OSGi. This was originally a big OSGi selling point if I recall correctly though I’ve never seen it successfully implemented - though maybe in the Eclipse IDE? Anyway it’s a very well studied and covered topic in the Java ecosystem as I understand it... http://ssw.jku.at/dcevm/ http://ssw.jku.at/dcevm/ http://blog.ruwan.org/2012/12/dynamic-hot-swap-environment-inside.html http://blog.ruwan.org/2012/12/dynamic-hot-swap-environment-i... https://www.jrebel.com/blog/java-hotswap-guide https://www.jrebel.com/blog/java-hotswap-guide
- bjourne 6y agoThe languages you mention can't update data in flight, which is something Erlang can. Suppose you have the following code running in a thread: def print_name(d): print(d['name']) d = {'name' : 'hello'} while True: print_name(d) Now you hot hot-swap it while the while-loop is running: def print_name(d): print('hello', d['first_name']) d = {'first_name' : 'there'} while True: print_name(d) Erlang can do this. No other language that I'm aware of can.
- dig1 6y agoClojure has all sorts of facilities to do that (atoms, agents or refs). You can even listen for changes [1] and compare old and new values. [1] https://clojuredocs.org/clojure.core/add-watch https://clojuredocs.org/clojure.core/add-watch
- bjourne 6y agoInteresting, I didn't know that. I don't understand how the function you link to can be used to accomplish that. Can you give me an example?
- dig1 6y agoSure; here is example with atom: ;; define a map (def myvar (atom {:foo 1})) ;; add watcher for 'myvar'; it will call anonymous ;; function and set old-state with previous value and new-state ;; with new value, when 'myvar' was changed (add-watch myvar :dummy (fn [_ _ old-state new-state] (println "changed! - old: " old-state " new: " new-state))) ;; Run separate thread and and update :foo value to 2, after 6 seconds. ;; No scheduling is necessary, this is so it can be evaluated in REPL easily. (future (Thread/sleep 6000) (swap! myvar assoc :foo 2)) ;; Infinite loop. Sleep for 0.5 seconds just for printing purposes. (loop [] (println @myvar) (Thread/sleep 500) (recur)) and you will see output like this: {:foo 1} {:foo 1} {:foo 1} {:foo 1} {:foo 1} {:foo 1} {:foo 1} {:foo 1} changed! - old: {:foo 1} new: {:foo 2} {:foo 2} {:foo 2} {:foo 2} {:foo 2} {:foo 2} {:foo 2}
- junke 6y agoCommon Lisp (also, you can change the class of existing objects).
- nonbirithm 6y agoIt's relatively easy to do in Lua, depending on how you organize modules. Imported libraries are just plain tables (dictionaries) instead of a language-level feature, so hot swapping means just clearing the table and replacing its contents with the ones loaded from disk. I think it is easier to do because there is less magic in the modules system; it's implemented on top of the language itself. I've used this technique in multiple games written in Lua, as well as a GUI app written with wxLua, and I can't imagine programming a long-running program in Lua again without it. There are many caveats like stale module local state but they can be circumvented by designing the program from the start to put such state into a separate module which gets preserved. You do also have to modify your program's code to accommodate hot swapping in some cases but I would say the effort is repaid many times over in time saved not having to restart the program and go all the way back to part you're testing again every time you make a change. I really wish it were easier to do in popular languages like Python but because of the way it interacts with imports I feel like your only chance to get it right is in the early stages of language design. One time I was wanting to program games in Typescript to take advantage of gradual typing but couldn't figure out an equivalent way to do hot reloading like in Lua, so I went with Lua instead. I feel like if more people knew how much benefit it brings from using a language that supports it we might see more mainstream support in new dynamic languages.