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

  • Home
  • SEARCH
  • 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 9184367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:01:49+00:00 2026-06-17T19:01:49+00:00

Possible Duplicate: Python: single instance of program I need to prevent a cron job

  • 0

Possible Duplicate:
Python: single instance of program

I need to prevent a cron job from running concurrent instances when a job takes longer to complete than the launcher interval. I’m trying to use the flock concept to achieve this, but fcntl module is not behaving the way I expect.

Can anyone tell me why this works to prevent two concurrent instances:

import sys
import time
import fcntl

file_path = '/var/lock/test.py'
file_handle = open(file_path, 'w')

try:
    fcntl.lockf(file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
    print 'no other instance is running'
    for i in range(5):
        time.sleep(1)
        print i + 1

except IOError:
    print 'another instance is running exiting now'
    sys.exit(0)

And why this does not work:

import sys
import time
import fcntl

def file_is_locked(file_path):
    file_handle = open(file_path, 'w')
    try:
        fcntl.lockf(file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
        return False
    except IOError:
        return True

file_path = '/var/lock/test.py'

if file_is_locked(file_path):
    print 'another instance is running exiting now'
    sys.exit(0)
else:
    print 'no other instance is running'
    for i in range(5):
        time.sleep(1)
        print i + 1
  • 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-06-17T19:01:50+00:00Added an answer on June 17, 2026 at 7:01 pm

    My humble opinion (although I may be totally wrong) is that file_handle is local to the function (in the second case) and therefore, it gets destroyed once the function is done.

    The following code seems to work as expected:

    #!/usr/bin/env python
    #http://stackoverflow.com/questions/14406562/prevent-running-concurrent-instances-of-a-python-script
    
    import sys
    import time
    import fcntl
    
    file_handle = None
    
    def file_is_locked(file_path):
        global file_handle 
        file_handle= open(file_path, 'w')
        try:
            fcntl.lockf(file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
            return False
        except IOError:
            return True
    
    file_path = '/var/lock/test.py'
    
    if file_is_locked(file_path):
        print 'another instance is running exiting now'
        sys.exit(0)
    else:
        print 'no other instance is running'
        for i in range(5):
            time.sleep(1)
            print i + 1
    

    Notice that the only thing I did is setting file_handle as global variable (although I copied the whole code to have a working example)

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

Sidebar

Related Questions

Possible Duplicate: Python read a single character from the user I am using below
Possible Duplicate: Python read a single character from the user I am looking to
Possible Duplicate: Python code to pick out all possible combinations from a list? I
Possible Duplicate: Substitute multiple whitespace with single whitespace in Python trying to figure out
Possible Duplicate: Single quotes vs. double quotes in Python I have seen that when
Possible Duplicate: Python splitting strings and jumbling the middle I have a program in
Possible Duplicate: python takes list and returns only if negative value also exists using
Possible Duplicate: Single quotes vs. double quotes in Python Usually we represent , create
Possible Duplicate: Best way to strip punctuation from a string in Python I have
Possible Duplicate: Python row, column, matrix trouble I'm writing a python program with with

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.