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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:25:28+00:00 2026-06-14T05:25:28+00:00

I have an application that at a given intervall loops through a list of

  • 0

I have an application that at a given intervall loops through a list of items (then length of this list varies) for each item it does a rather simple operation, it’s not just adding a value but neither is it some really complex computations.

What I’m wondering is should i lock for each item like this (current solution):

def method_1:
    for item in the_list:
        do_operation(item);

def do_operation(item):
    lock()
    //do some stuff.
    unlock()

Or should I do it like this:

def method_1:
    lock()
    for item in the_list:
        do_operation(item);
    unlock()

def do_operation(item):
    //do some stuff.

I guess this is actually really hard to answer since i assume that much depends on what “do some stuff” is and how long it takes. And I honestly don’t know how much time this takes. Especially compared to how long it takes for python to aquire the lock.

Let me know in the comments if I could improve my question somehow.

  • 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-14T05:25:29+00:00Added an answer on June 14, 2026 at 5:25 am

    I’d suggest locking for the entire list (option 2). If you only lock for each item the list could be modified partway through the operation, which could be problematic depending on what exactly you are doing. There are also overheads associated with each lock and unlock.

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

Sidebar

Related Questions

I have this application that will recurse all folders in a given directory and
cowboy coder needs some help from SO-veterans: I have a given application that uses
In my WPF application I have a View that is given a ViewModel, and
Given that the web application doesn't have su privileges, I'd like to execute a
I have made an application that gives the user the option to open up
Lets say that you have a following simple application: <form action=helloServlet method=post> Give name:<input
I have two spreadsheets that each give information about a bunch of applications running
I have application that makes different queries with different results so the caching in
I have application that is connecting to the DB and if I enter incorrect
I have application that is up more than 3 days. I can see in

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.