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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:05:31+00:00 2026-06-16T05:05:31+00:00

The code flow is something as below. result = [] def Discover(myList=[]): for item

  • 0

The code flow is something as below.

result = []
def Discover(myList=[]):
    for item in myList: 
        t = threading.Thread(target=myFunc, Args=[item])
        t.start()

def myFunc(item):
    result.append(item+item)

Now this will start multiple threads and in current scenario the threads does some memory intensive Tasks. Thus I want to include semaphores in this so that myList behaves as a queue and number of threads must be in a limited size. What is the better way to do that?

  • 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-16T05:05:32+00:00Added an answer on June 16, 2026 at 5:05 am
    1. Never use mutable objects as default parameter value in function definition. In your case: def Discover(myList=[])

    2. Use Queue.Queue instead of list to provide myList if it’s necessary to update list of “tasks” when threads are running. Or… Use multiprocessing.pool.ThreadPool in order to limit number of running threads at the same time.

    3. Use Queue.Queue instead of list to provide results variable. list implementation is not thread-safe, so you probably will get many problems with it.

    4. You can find some examples in other SO questions, i.e. here.

    P.S. ThreadPool available in Python 2.7+

    $ python
    Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
    [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from multiprocessing.pool import ThreadPool
    >>> ThreadPool
    <class 'multiprocessing.pool.ThreadPool'>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why gose to image's flow This is code: <p><img class=left width=95 height=64 src=# />
I am tired of debugging a flow of my official project code - it
I just read in Code Complete that you should not use exceptions for flow
I'm reviewing some team code and I found something like this : MyObj obj
I've got a code work flow dependent on pulling changes from a repo clones
Shoes.app do flow do file = something with variable length para Loading #{file}: progress
I came across this code: for (final String s : myList) { s.equalsIgnoreCase(test); updateNeeded
In this javascript code f.init() control the entire flow of the program: var f
Is there any software that visualizes algorithms from code? As a flow chart of
I want to watch the flow the execution of my ruby code when I

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.