Re: [psychopy-users] Using psychopy from a gui ... how to stop psychopy

57 views
Skip to first unread message

Jeremy Gray

unread,
Jan 13, 2016, 9:00:20 AM1/13/16
to psycho...@googlegroups.com
[moving to dev list]

This has me wondering if we might benefit from the axexit standard module (https://docs.python.org/2/library/atexit.html). Its purpose is to handle clean-up and similar functions (e.g., data saving).

Right after importing psychopy.core, we would do

axexit.register(core.quit)

At the end of Window.__init__(), we could add 

atexit.register(self.close)

Then at normal program end or upon sys.exit(), the registered functions will automatically get called (in reverse order of their registration order).

pyo could register its server shutdown stuff. (I know its gets called from within __del__, but there have been some shutdown problems recently, lack of proper shutdown, so maybe this could help).

Just an idea.



--Jeremy

On Wed, Jan 13, 2016 at 12:20 AM, Michael MacAskill <michael....@nzbri.org> wrote:
Hi Jason,

I think core.quit() does some PsychoPy housework before exiting (e.g. ensuring all data saving is completed, files closed etc). You might want to check that you aren't missing out on any of that (probably less of an issue if you are writing your own experiments rather than using Builder, but still…)

Regards,

Michael


> On 13/01/2016, at 15:22, Jason Steffener <stef...@gmail.com> wrote:
>
> I figured it out using sys.exit()
>
> # check for quit (the Esc key)
>             if endExpNow or event.getKeys(keyList=["escape"]):
>                 win.close()
>                 sys.exit()
>
> Thanks for the clues Jeremy.
>
> Jason

--
Michael R. MacAskill, PhD               66 Stewart St
Research Director,                      Christchurch 8011
New Zealand Brain Research Institute    NEW ZEALAND

Senior Research Fellow,                 michael....@nzbri.org
Te Whare Wānanga o Otāgo, Otautahi      Ph:   +64 3 3786 072
University of Otago, Christchurch       http://www.nzbri.org/macaskill

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/16E17460-9239-4E2C-A234-1DB4210ECF65%40nzbri.org.
For more options, visit https://groups.google.com/d/optout.

Michael MacAskill

unread,
Jan 13, 2016, 8:05:11 PM1/13/16
to psycho...@googlegroups.com
Hi Jeremy,

That sounds like it could be useful, if I understand that correctly.

Actually the core.quit() function doesn't do much (see below). I guess the data saving and file closing stuff is handled automatically by the Trial/ExperimentHandler instances when they are de-allocated.


def quit():
"""Close everything and exit nicely (ending the experiment)
"""
#pygame.quit() #safe even if pygame was never initialised
logging.flush()
for thisThread in threading.enumerate():
if hasattr(thisThread,'stop') and hasattr(thisThread,'running'):
#this is one of our event threads - kill it and wait for success
thisThread.stop()
while thisThread.running==0:
pass#wait until it has properly finished polling

sys.exit(0)#quits the python session entirely



Regards,

Michael

Jonathan Peirce

unread,
Jan 14, 2016, 7:01:18 AM1/14/16
to psycho...@googlegroups.com
I agree. Then we could call sys.exit() as the standard call to end,
rather than core.quit(). And this could force shutdown of several
objects to run their __del__() method avoiding some of the issues we
currently have.

For instance, if an ExperimentHandler ends up with a circular reference
with something then its __del__ method doesn't get called because
garbage collection fails to pick it up. When that happens
del myExp #fails
myExp.__del__() #succeeds and will force the data save to occur
So we could register really important things like ExperimentHandlers (as
a list of weak references) in psychopy.core and then force a call to
their __del__ methods during sys.exit. Alternatively, yes, registering
the shutdown methods for each object with atexit would be another way to
go. What happens then if the object has been deleted before the sys.exit
is called? Do we have to remember to de-register?

2 questions - minor issues that we should just check:
- if we have sys.exit() in core.quit and core.quit is handled by
atexit, does Python detect/avoid the circularity?
- does this play nicely with the calls to __del__ for our existing
objects? (I imagine it does and possibly works better)

cheers
Jon
--
Jonathan Peirce
University of Nottingham

http://www.peirce.org.uk





This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please send it back to me, and immediately delete it.

Please do not use, copy or disclose the information contained in this
message or in any attachment. Any views or opinions expressed by the
author of this email do not necessarily reflect the views of the
University of Nottingham.

This message has been checked for viruses but the contents of an
attachment may still contain software viruses which could damage your
computer system, you are advised to perform your own checks. Email
communications with the University of Nottingham may be monitored as
permitted by UK legislation.

Jeremy Gray

unread,
Jan 14, 2016, 8:50:23 AM1/14/16
to psycho...@googlegroups.com
If there are circularity issues with calling sys.exit(0) in core.quit(), it should be possible to use os._exit() at that point. os._exit() sidesteps all atexit business. sys.exit just raises a SystemExit exception, which can then be caught and handled. 

Not sure how this would work with the existing __del__ calls, probably quite smoothly. 

--Jeremy



--
You received this message because you are subscribed to the Google Groups "psychopy-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-dev...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages