diff --git a/timelapse/__main__.py b/timelapse/__main__.py index 00d6cdb..0aa4d68 100644 --- a/timelapse/__main__.py +++ b/timelapse/__main__.py @@ -40,6 +40,7 @@ def applicationDidFinishLaunching_(self, notification): # Initialize recording self.recording = start_recording + self.recorder = None # Set correct output paths self.recorder_output_basedir = os.path.join( @@ -96,7 +97,7 @@ def createMenu(self): def startStopRecording_(self, notification): if self.recording: - self.recorder.join() + self.recorder.join(timeout=screenshot_interval*2) # Create timelapse after recording? if encode: self.encoder = Encoder( diff --git a/timelapse/recorder.py b/timelapse/recorder.py index 91c52ed..dac7f04 100644 --- a/timelapse/recorder.py +++ b/timelapse/recorder.py @@ -1,19 +1,20 @@ import os # Taking screenshot import datetime # Filename -from threading import Thread, Event # Recorder is a thread +from multiprocessing import Process, Event -class Recorder(Thread): +class Recorder(Process): """ Takes a screenshot every 'interval' seconds and saves it into output_dir or a subdirectory thereof. """ def __init__(self, output_dir, interval=4): # Initialize the thread - Thread.__init__(self) + Process.__init__(self) # Provide a way to stop the recorder self._stop = Event() + self._stopped = Event() # Set config options self.output_dir = output_dir @@ -27,15 +28,18 @@ def __init__(self, output_dir, interval=4): def join(self, timeout=None): """ Stop recording """ self._stop.set() + self._stopped.wait() print("Recorder stopped. Total recording time: " + self.get_recording_time() + ".") - Thread.join(self) + Process.join(self, timeout=timeout) def run(self): """ Periodically take a screenshots of the screen """ - while not self._stop.isSet(): + while not self._stop.is_set(): self.screenshot() self._stop.wait(self.interval) + self._stopped.set() + def get_recording_time(self): return str(self.screenshot_counter * self.interval) + " seconds"