Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6971407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:50:11+00:00 2026-05-27T16:50:11+00:00

I am using file locking in python script (to control single instance of it

  • 0

I am using file locking in python script (to control single instance of it execution).
http://code.google.com/p/pylockfile/
I release lock in finally code block.
But if script closed, for example, closing the terminal running it, the finally block doesn’t execute and the file stays locked.

How to catch python script destructor event in any case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:50:11+00:00Added an answer on May 27, 2026 at 4:50 pm

    See this blog post regarding this subject. It uses the win32api when under Windows, while under Linux the SIGTERM signal is caught. To verify its working, it might be helpful to write something to a file in the on_exit handler like as done below. As the snippet is quite brief, I’ll just include it (full props to the blog author):

    import os, sys
    def set_exit_handler(func):
        if os.name == "nt":
            try:
                import win32api
                win32api.SetConsoleCtrlHandler(func, True)
            except ImportError:
                version = '.'.join(map(str, sys.version_info[:2]))
                raise Exception('pywin32 not installed for Python ' + version)
        else:
            import signal
            signal.signal(signal.SIGTERM, func)
    
    if __name__ == '__main__':
        def on_exit(sig, func=None):
            f = open('log.txt', 'w')
            f.write('shutdown...')
            f.close()
            sys.exit()
        set_exit_handler(on_exit)
        print 'Press  to quit'
        raw_input()
        print 'quit!'
    

    If you will close the terminal running that program, it will create a file to verify the callback functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large tar.gz file to analyze using a python script. The tar.gz
I'm trying to write a Python script for searching out duplicate mp3/4 files using
I find myself using python for a lot of file management scripts as the
I'm trying to get a python script of mine to print a CSV file
I am trying to run an awk script using python, so I can process
I'm trying to write an array (list?) to a text file using Python 3.
I’m trying to write a python script that sends a query to TweetSentiments.com API.
Python's fcnt module provides a method called [flock][1] to proved file locking. It's description
I'm looking to parse a xml file using Python and I was wondering if
I'm looking for solution to extract some node from large xml file (using xmlstarlet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.