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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:56:14+00:00 2026-06-17T03:56:14+00:00

I currently have a global Lock = threading.Lock() , and make the following call:

  • 0

I currently have a global Lock = threading.Lock(), and make the following call:

Parallel(n_jobs=2)(delayed(serialRemove)(dir,c,b,l,f) for f in os.listdir(dir))

using jobLib. In serialRemove, I have

Lock.acquire()
print(f+' begin')
if h in hashes:
    try:
        os.remove(path)
        if l: print('Removing ' + path)
        removed += 1
    except os.error:
        print('Encountered error removing file') 
else:
    hashes.add(h)
print(f+' end')
Lock.release()

Part of the call results in:
10.txt begin
11.txt begin
20.txt begin
I don’t understand how there could be two begin prints if I surround the code in a Lock. Is there any easy way to protect the code block so ideally I get:
10.txt begin
10.txt end
11.txt begin
11.txt end
20.txt begin
20.txt end

  • 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-17T03:56:16+00:00Added an answer on June 17, 2026 at 3:56 am

    threading.Lock only works between threads of the same process.

    Without actually knowing what library you’re using for parallelism here, it’s hard to be sure, but it’s almost certainly executing the tasks in separate processes. (Anything that starts threads in the same process, at least with CPython, isn’t going to get any effective parallelism for CPU-bound code, because of the GIL. Therefore, none of them do that.)

    So, if you try to use a global threading.Lock object from other processes, you’re going to get a completely independent lock in each process. So, locking it doesn’t do any good. (With some parallel libraries—possibly different on each platform—you’ll get an error instead. But there’s no way it could possibly do what you want.)

    Most parallelization libraries have their own lock types that work with their style of multiprocessing. If yours does, use the one that comes with your library.

    If not, depending on how your library works, multiprocessing.Lock may do the trick.

    If not, you’ll have to implement something explicitly using, e.g., a lock file (possibly together with flock/lockf, or relying on Windows exclusive open, or whatever).

    Also, note that at least one of the multiple libraries that has an API that could make sense of your example line of code, [joblib], is explicitly designed for tasks that do not have any sharing, and therefore isn’t supposed to work with locks at all. (It probably will work with multiprocessing.Lock anyway, but you really shouldn’t count on that.)

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

Sidebar

Related Questions

Currently making a global volunteer site, and the webpages will more or less have
I currently have one project that currently contains multiple packages. These packages make up
I currently have code like this: cache = 1 def foo(): global cache #
I currently have a Web Application that runs off a global Javascript-based API, and
I have a global variable called fWidth that is currently 300. I'm setting pictureBox.width
I have code that is completely parallel, no dependencies, so using pthreads was a
I currently have a Web Application which is using it's own Permissions table which
How do I lock mysql tables in php? I currently have this code: $db->query(LOCK
I currently have to implement a query on a postgres database using a prepared
I currently have the following Javascript, and I am not sure what I should

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.