question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dev: hot-reloading newm's own source?

See original GitHub issue

First, thanks! newm is both innovative and (together with pywm) probably the easiest starting point that now exists for hackable wayland WM 👏

Do you have a recipe for reloading newm source itself?

Things I found out today:

  • I can reload() modules and log stuff from config file. Poor man’s REPL — edit config, press hot-key, see output in tail -f $HOME/.cache/newm_log | grep -v 'GL_INVALID_VALUE|TIMER|DEBUG|wm_output.c' (I tweak the command to whatever I consider noise at the moment) 🤣

  • It’s convenient to do things in on_reconfigure function so the output appears closer to the end. (OTOH if I were hacking on logic such as _setup_widgets I’d maybe want to reload it before it runs? EDIT: see below, this proved better)

  • As usual in Python, reload()ing modules is not enough when other modules:

    1. already have instances of them.
    2. worse, have code running in threads!
    3. have copies e.g. via from mod import var, Class.

    At least for widgets, (1) (2) are luckily covered by layout.py stopping threads, destroying and re-creating instances.

    (3) is a problem, specifically from .widget import TopBar, BottomBar, Background, Corner, FocusBorders means layout modules would still use references to previous class object to re-create instances. I now came up with a quick hack in config file that partially monkey-patches such copies, enough to let me modify widgets appearance and seeing the results without newm restart:

def on_reconfigure():
    logger.warning('~' * 120)
    
    import sys, importlib

    mod_names = ['newm.widget.bar', 'newm.widget.focus_border', 'newm.layout']
    for mod_name in mod_names:
        if mod_name in sys.modules:
            importlib.reload(sys.modules[mod_name])

    for mod_name in mod_names:
        for name, obj in vars(sys.modules[mod_name]).items():
            source_mod_name = getattr(obj, '__module__', None)
            if source_mod_name and source_mod_name != mod_name and source_mod_name in sys.modules and 'newm' in source_mod_name:
                current_obj = getattr(sys.modules[source_mod_name], name, None)
                if type(obj) == type(current_obj) and obj is not current_obj:
                    logger.warning('RELOAD PATCHING %s.%s \t<- %s = %r', mod_name, name, source_mod_name, current_obj)
                    setattr(sys.modules[mod_name], name, current_obj)
   
    logger.warning('_' * 120)

I’m sure a better stuff is possible, there are auto-reload modules that do much more intelligent patching…
Do you have a ready setup that you’re using? Or do you always start-newm?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
CRAG666commented, Jun 28, 2022
0reactions
cbencommented, Jul 8, 2022

if you are asking how it works without my kludge:

newm’s current config reload flow

Additionally, all config params are used like this:

conf_enable_dbus_gestures = configured_value("gestures.dbus.enabled", True)

which returns a callable; wherever code wants to actually use the value it calls it e.g. if conf_enable_dbus_gestures(): so it always gets a fresh value.
And config params that specify a callback get used like conf_foo()(args).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack dev server, Hot reload and source maps - Medium
Let's heat things up with hot reload. Try to run npm run start:dev, change something in the code while keeping an eye on...
Read more >
Hot Reload - Garden
This command is executed inside the running container during each hot reload, after syncing is completed (as the name suggests). Following is a...
Read more >
Create a live-reload server f | Butterscotch Shenanigans
The goal of hot reloading is to refresh the minimum possible portion of content ... We'll make a simple, live-reload development server that ......
Read more >
Creating a Productive Local Development Environment with ...
Ready to build your own productive local development environment? · Deploy a service from source code to a Kubernetes cluster · Route traffic...
Read more >
How we enable hot-reloading in production for extension ...
The client side of hot reloading is watching the file system for changes to the extension source files and running the equivalent of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found