From 92e5df0b8981d85a2cc9f7550cde12e368ca0815 Mon Sep 17 00:00:00 2001 From: Matt Arnold Date: Thu, 4 Sep 2025 10:08:47 -0400 Subject: [PATCH] add extensive commentary on daemonize code --- nunnally.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/nunnally.py b/nunnally.py index 1e6615d..15a4890 100755 --- a/nunnally.py +++ b/nunnally.py @@ -39,6 +39,8 @@ class NunnallyService(dbus.service.Object): @dbus.service.method("club.breadpunk.nunnally") def ocrImage(self): + if not self.image_targets(): + return False pixbuf = self.boardxs.wait_for_image() if pixbuf is not None: self.work_img.set_from_pixbuf(pixbuf) @@ -68,25 +70,69 @@ class NunnallyService(dbus.service.Object): im = Image.frombytes(mode, (w, h), data, "raw", mode, stride) return im + def image_targets(self): + # my instincts are telling me to optimize this + targets = self.boardxs.wait_for_targets() + image_targets = [ + "image/png", + "image/jpeg", + "image/jpg", + "image/gif", + "image/bmp", + "image/tiff", + "image/webp", + ] + + target_strings = [str(target) for target in targets[1]] + + # + for image_target in image_targets: + if image_target in target_strings: + return True + + return False + pid = os.fork() # Hmmm this looks an awful lot like... C +# Yes it does per C fork(3) creates a nearly identical copy of the +# calling process as a child of the calling process +# returning it's pid to the caller. Asexual reproduction at it's finest + if pid: - os._exit(0) # kill the parent + os._exit(0) + # exit without running exit handlers, that might cause race condition + # in the child + + +# Per the fork manual the child begins execution at the point where fork +# is called, as if the child had called it. The only difference being +# is the child process gets a zero as return value, and so the else branch +# of this if is followed. else: - # directions say this will stop exceptions while - # daemonized. Which would be bad + # It turns out my CS prof lied about the purpose of these two calls + # the child process needs to be the process group leader, when parent + # exits or it gets reaped by the init system os.setpgrp() os.umask(0) print(os.getpid()) # to aid in stopping the server - # Run silent, run deep + # We want to close our connection to the controlling terminal + # to avoid accedentially spamming the use. And causing interactive processes + # to be SIGSTOP'ed. I do this with a Null Device class. + # You could just as easily do some sort of logging thing. sys.stdin.close() sys.stdout = NullDevice() sys.stderr = NullDevice() +# The last thing we do before handing things off to the daemon's main +# function is set up the daemon's signal table how we want it +# fork, may have initialized it with the default handlers +# depending on implementation signal.signal(signal.SIGHUP, hup_handle) signal.signal(signal.SIGTERM, hup_handle) + +# And the main loop begins DBusGMainLoop(set_as_default=True) myservice = NunnallyService() Gtk.main()