Last active
October 2, 2025 18:53
-
-
Save xealits/9365b19882ba411711e39e7d723074e4 to your computer and use it in GitHub Desktop.
C hotreload/figwheel in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import ctypes | |
from ctypes import cdll | |
cur_x = 10 | |
libpath = "./some_lib.so" | |
lib = cdll.LoadLibrary(libpath) | |
print(f"loaded {lib}") | |
# https://stackoverflow.com/a/62021495/1420489 | |
def ctypesCloseLibrary(lib): | |
dlclose_func = ctypes.CDLL(None).dlclose | |
dlclose_func.argtypes = [ctypes.c_void_p] | |
dlclose_func.restype = ctypes.c_int | |
dlclose_func(lib._handle) | |
def reloadLibrary(lib): | |
name = lib._name | |
ctypesCloseLibrary(lib) | |
return cdll.LoadLibrary(name) | |
while True: | |
inp = input("> ") | |
inp = inp.strip().upper() | |
if inp == "X": | |
x = lib.foo(cur_x) | |
print(x) | |
elif inp == "L": | |
lib = reloadLibrary(lib) | |
print(f"reloaded {lib}") | |
else: | |
print(f"unknown input {inp}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just to write it down: