473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

cleanup actions on sys.exit()

4
Hello,
I wrote a GUI app in Jython (using Java Swing) which uses database. The application remains connected for its whole run time until the main window is closed (JFrame, using defaultCloseOperation = JFrame.EXIT_ON_CLOSE).
Now, when exiting, I would like to make sure I explicitly close the database connection. Is there any function, like _atexit() in C, where I could place my code?
Thank you very much.
Apr 16 '12 #1

✓ answered by dwblas

I use the __del__ function however because of the way the Python garbage collector uses a count of references, you may have to be explicit on closing some objects, like file pointers.
http://eli.thegreenplace.net/2009/06...ors-in-python/

2 2303
dwblas
626 Expert 512MB
I use the __del__ function however because of the way the Python garbage collector uses a count of references, you may have to be explicit on closing some objects, like file pointers.
http://eli.thegreenplace.net/2009/06...ors-in-python/
Apr 18 '12 #2
Jindra
4
Oh, yes, this is what I was looking for but could not find! :) Thank you, dwblas!
Strangely, I haven't heard of __del__ since I first learn some python (version 1.6, I guess).
In the meantime, I found there is one more way in Swing, and that is Runtime.addShutdownHook(Thread).
I defined this function:
Expand|Select|Wrap|Line Numbers
  1. class ShutdownTask( java.lang.Thread ):
  2.     def __init__( self, connection ):
  3.         self.conn = connection
  4.         print 'ShutdownTask setup: object = %s' % connection
  5.     def run( self ):
  6.         print 'ShutdownTask in progress'
  7.         if self.conn is not None:
  8.             try:
  9.                 print 'ShutdownTask: shutting down database connection'
  10.                 self.conn.conn.close()
  11.                 print 'ShutdownTask: connection closed'
  12.             except Exception, e:
  13.                 print 'ShutdownTask: exception'
  14.                 print e
  15.         print 'ShutdownTask: done'
  16.  
and in the init thread I call this:
Expand|Select|Wrap|Line Numbers
  1.     Q = Connection() # my class to represent database connection
  2.     shutdownTask = ShutdownTask( Q )
  3.     java.lang.Runtime.getRuntime().addShutdownHook( shutdownTask )
  4.  
Works perfectly and is "Swing native", but __del__ in the main GUI class should do the same job.
Apr 18 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Ivan Voras | last post by:
In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a...
3
by: Maxwell Hammer | last post by:
An application I am developing executes many threads and then has a "monitor" part that waits for certain events. One of these events causes the application to have to shutdown. On shutdown the...
1
by: Guinness Mann | last post by:
Pardon me if this is not the optimum newsgroup for this post, but it's the only .NET newsgroup I read and I'm certain someone here can help me. I have a C# program that checks for an error...
15
by: lallous | last post by:
Hello, I have a function like: void fnc() { char *mem1, *mem2, *mem3, *mem4; // lots of code... // mem1, 2, 3, 4 got allocated // lots of code and condition checks if (condition_failed)
12
by: Siemel Naran | last post by:
(1) About std::exit. If I call this function, will the system call the destructors of all objects in the call stack. Eg. void f() { std::exit(1); } void g() { X x; f(); } Does the call in...
10
by: lallous | last post by:
Hello, This question was asked in comp.lang.c++ and the answers involved the use of objects whose destructors are automatically called when getting out of scope, however I was expecting...
2
by: gl | last post by:
I currently have an asp.net web app that starts a thread in the global.asax app start method. THe thread then runs for the duration of the app's life, changing data in the background. My...
0
by: Jacob Kroon | last post by:
Hi, I'm working on a library written in C using GObject, which i provide python bindings for using pygtk. When I checked the library for memory leaks using valgrind, I noticed that none of the...
3
by: Petr Pavlu | last post by:
Hello, I have two questions how the functions should be written. I read the FAQ but didn't find any answer. If there is any please point me out. I. Cleanup code Consider I have to open file1,...
16
by: Laurent Deniau | last post by:
I would like to know if the use of the pointer ref in the function cleanup() below is valid or if something in the norm prevents this kind of cross-reference during exit(). I haven't seen anything...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.