2 ms·
I used different thread just for printing/add-watch purposes. But, using myvar or your-like example (hot loading function) in the same thread works without any
by dig1 6y ago
I used different thread just for printing/add-watch purposes.
But, using myvar or your-like example (hot loading function) in the same thread works without any problems as well - put function in a loop, change function body and reload namespace where myvar/function exists - loop will pick that up instantly.
- bjourne 6y agoIn your example, you modified the assoc while another thread was accessing it. That is a standard feature of languages with threading support and is not impressive. Let me give you a clearer example than my first one: def fn(): d = {'name' : 'hello'} while True: print(d['name']) threading.Thread(target=fn).run() This causes a thread to be started which prints "hello" indefinitely. Clojure cannot, while the thread is running, change the execution to, say: def fn(): d = {'name' : 'there'} while True: print(d['name']) time.sleep(0.5) Clojure cannot change the value of the local variable d because it has no reference to it. Clojure cannot add the line time.sleep(0.5) to the loop because it has no facility for "upgrading" running threads.
- dig1 6y agoCheck [1] to see what I'm talking about. I'd suggest you to investigate more before making such bold claims. [1] http://webcache.googleusercontent.com/search?q=cache:ieBuArSKu2MJ:travis-whitton.blogspot.com/2009/09/hot-code-swapping-with-clojure.html+&cd=3&hl=en&ct=clnk&gl=ba http://webcache.googleusercontent.com/search?q=cache:ieBuArS...
- bjourne 6y agoYou are quite arrogant. He reloads and rebinds functions and symbols. That is not the same thing as hot-swapping functions. Hot-swapping means that the function is running and in his example no running function is replaced. Had the main function looked like this: (defn main [] (let [what "blah"] (loop [] (println what) (Thread/sleep 1000) (recur)))) It would neither have been possible to change the output nor the sleep time.
- deleted 6y ago[deleted]
- fulafel 6y agoReading up on the Erlang system, it doesn't upgrade function-local state this way either. It relies on a convention where this state is stored externally to the function by the gen_server framework and passed in, right? (going by https://stackoverflow.com/questions/1840717/achieving-code-swapping-in-erlangs-gen-server https://stackoverflow.com/questions/1840717/achieving-code-s...)
- bjourne 6y agoYes and no. Like everything in Erlang, hot-swapping relies on code being structured as servers that responds to messages. In this case, the VM sends a message to the process telling it about the code upgrade. The process has to handle this message "manually" and do what it needs with it. For example, changing its internal state or updating database schema if it uses external storage. But you don't have to use the gen_server framework to accomplish this. It's perfectly fine (but not recommended as there are lots of details you want to get right) to write your own code for handling hot-swapping. See http://erlang.org/documentation/doc-4.9.1/doc/design_principles/gen_server.html http://erlang.org/documentation/doc-4.9.1/doc/design_princip... for some details on server principles. Immutability in combination with Erlang's process centric view is what makes it possible.
- fulafel 6y agoThanks for the explanation. There's an idea in there for a Clojure library :)