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 3211544
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:44:41+00:00 2026-05-17T14:44:41+00:00

Python’s fcnt module provides a method called [flock][1] to proved file locking. It’s description

  • 0

Python’s fcnt module provides a method called [flock][1] to proved file locking. It’s description reads:

Perform the lock operation op on file
descriptor fd (file objects providing
a fileno() method are accepted as
well). See the Unix manual flock(2)
for details. (On some systems, this
function is emulated using fcntl().)

Looking up the linux man page for flock, it only refers to cross process locking, for example:

A call to flock() may block if an
incompatible lock is held by another
process. To make a non-blocking
request, include LOCK_NB (by ORing)
with any of the above operations.

So my question is: will flock() also provide thread safe locking and lock multiple threads within the same process as well as threads from different processes?

[1]: http://docs.python.org/library/fcntl.html#fcntl.flockfunction is emulated using fcntl().)

  • 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-17T14:44:41+00:00Added an answer on May 17, 2026 at 2:44 pm

    flock locks don’t care about threads–in fact, they don’t care about processes, either. If you take the same file descriptor in two processes (inherited through a fork), either process locking the file with that FD will acquire a lock for both processes. In other words, in the following code both flock calls will return success: the child process locks the file, and then the parent process acquires the same lock rather than blocking, because they’re both the same FD.

    import fcntl, time, os
    
    f = open("testfile", "w+")
    print "Locking..."
    fcntl.flock(f.fileno(), fcntl.LOCK_EX)
    print "locked"
    fcntl.flock(f.fileno(), fcntl.LOCK_UN)
    
    if os.fork() == 0:
        # We're in the child process, and we have an inherited copy of the fd.
        # Lock the file.
        print "Child process locking..."
        fcntl.flock(f.fileno(), fcntl.LOCK_EX)
        print "Child process locked..."
        time.sleep(1000)
    else:
        # We're in the parent.  Give the child process a moment to lock the file.
        time.sleep(0.5)
    
        print "Parent process locking..."
        fcntl.flock(f.fileno(), fcntl.LOCK_EX)
        print "Parent process locked"
        time.sleep(1000)
    

    On the same token, if you lock the same file twice, but with different file descriptors, the locks will block each other–regardless of whether you’re in the same process or the same thread. See flock(2): If a process uses open(2) (or similar) to obtain more than one descriptor for the same file, these descriptors are treated independently by flock(). An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor.

    It’s useful to remember that to the Linux kernel, processes and threads are essentially the same thing, and they’re generally treated the same by kernel-level APIs. For the most part, if a syscall documents interprocess child/parent behavior, the same will hold for threads.

    Of course, you can (and probably should) test this behavior yourself.

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

Sidebar

Related Questions

I need to develop a file indexing application in python and wanted to know
If I write a python script using only python standard libraries, using Python 2.6
I'm trying to build a C++ extension for python using swig. I've followed the

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.