Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
BF: some machines weren't saving data correctly mid-experiment
Must have been something to do with ExperimentHandler not cleaning itself
up (maybe due to a circular reference somewhere?)

Adding the use of atexit as a second safety measure but keeping __del__
as well.

This commit is taking the code from
  89263ae
and
  24d3d83
but can't cherry-pick those due to restructured code
  • Loading branch information
peircej committed Dec 7, 2017
1 parent c555ca2 commit 193ce54
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions psychopy/data.py
Expand Up @@ -18,6 +18,7 @@
from scipy import optimize, special
import inspect # so that Handlers can find the script that called them
import codecs
import atexit
import weakref
import re
import warnings
Expand Down Expand Up @@ -112,6 +113,7 @@ def __init__(self,
autoLog : True (default) or False
"""
ExperimentHandler._instances.add(self)
self.loops = []
self.loopsUnfinished = []
self.name = name
Expand All @@ -137,16 +139,11 @@ def __init__(self,
else:
# fail now if we fail at all!
checkValidFilePath(dataFileName, makeValid=True)
# make sure we close and save data when Python exits
atexit.register(self.close)

def __del__(self):
if self.dataFileName not in ['', None]:
if self.autoLog:
logging.debug(
'Saving data for %s ExperimentHandler' % self.name)
if self.savePickle == True:
self.saveAsPickle(self.dataFileName)
if self.saveWideText == True:
self.saveAsWideText(self.dataFileName + '.csv', delim=',')
self.close()

def addLoop(self, loopHandler):
"""Add a loop such as a :class:`~psychopy.data.TrialHandler`
Expand Down Expand Up @@ -377,6 +374,23 @@ def saveAsPickle(self, fileName, fileCollisionMethod='rename'):
self.savePickle = savePickle
self.saveWideText = saveWideText

def close(self):
"""Cleanly close the experiment and save files if needed.
This method isn't usually needed by the user - it is called at exit
by the Python instance
"""
if self.dataFileName not in ['', None]:
if self.autoLog:
logging.debug(u'Saving data for %s ExperimentHandler'
% self.name)
if self.savePickle:
self.saveAsPickle(self.dataFileName)
if self.saveWideText:
self.saveAsWideText(self.dataFileName + '.csv', delim=',')
self.abort()
self.autoLog = False

def abort(self):
"""Inform the ExperimentHandler that the run was aborted.
Expand Down

0 comments on commit 193ce54

Please sign in to comment.