Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from cmangla/recorder-process
Browse files Browse the repository at this point in the history
Run Recorder in a process instead of thread.
  • Loading branch information
mre authored Nov 16, 2019
2 parents 7b15adf + 5943f45 commit 97552cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion timelapse/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 9 additions & 5 deletions timelapse/recorder.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"

Expand Down

0 comments on commit 97552cb

Please sign in to comment.