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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:17:42+00:00 2026-06-07T11:17:42+00:00

It’s the same as finder on Windows but use thread to get faster, import

  • 0

It’s the same as finder on Windows but use thread to get faster,

    import os,threading,multiprocessing


    def finder(path,q):
     for x in os.walk(unicode(path)):
      if x[1]:
       for dirname in x[1]:
        if target in dirname.lower():
         q.put(os.path.join(x[0],dirname))
      if x[2]:
       for name in x[2]:
        if target in name.lower():
         q.put(os.path.join(x[0],name))

     q.put(1)

    def printer(q):
     cmd=0
     while 1:
      tmp=q.get()
      if tmp==1:
       cmd += 1
       continue
      if cmd ==thnum:
       break
      print tmp

    if __name__ =="__main__":
     q=multiprocessing.JoinableQueue()
     ini=os.walk(u"C:\\").next()
     thnum=len(ini[1])
     target=raw_input("what you wanna get\n")

     p=multiprocessing.Process(target=printer,args=(q,))
     p.daemon=1
     p.start()

     for i in xrange(thnum):
      t=threading.Thread(target=finder,args=(ini[1][i],q,))
      t.start()
      print i," started"
     q.join()

it shows

0 started
1 started
….
22 started

but never shows the result
so my question is

  1. why doesn’t the result shows
  2. I know the code is dirty:(…is that a clean way to do it?

thank you guys.

  • 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-07T11:17:45+00:00Added an answer on June 7, 2026 at 11:17 am

    You have just a ton of messy code in here and also some errors. The major problem I see is that your threads are immediately failing to produce anything from their os.walk, and going right to exiting with the q.put. This is because you don’t pass a full path to each thread. Only a directory name. But its hard to know this because you dont use descriptive names for any variables.

    Here is a cleaned up version:

    import os
    import threading
    import multiprocessing
    
    
    def finder(path, q, done):
        for root, dirs, files in os.walk(unicode(path)):
            for dirname in dirs:
                if target in dirname.lower():
                    q.put(os.path.join(root,dirname))
            for name in files:
                if target in name.lower():
                    q.put(os.path.join(root,name))
    
        # print "Leaving thread", threading.current_thread()
        done.put(1)
    
    def printer(q,done,worker_count):
        total = 0
        while 1:
            try: done.get_nowait()
            except: pass
            else: total += 1
    
            if total == worker_count:
                break
    
            try: tmp=q.get(timeout=1)
            except: pass
    
            print tmp
    
    if __name__ =="__main__":
    
        results = multiprocessing.Queue()
        done = multiprocessing.Queue()
        root, dirs, files = os.walk(u"C:\\").next()
        thnum=len(dirs)
        target=raw_input("what you wanna get\n")
    
        p=multiprocessing.Process(target=printer,args=(results,done,thnum))
        p.start()
    
        for i in xrange(thnum):
            full_path = os.path.join(root, dirs[i])
            t=threading.Thread(target=finder,args=(full_path, results, done))
            t.start()
    
        p.join()
    

    See how I join the full path together in the main block before sending them off to each thread? I removed the JoinableQueue because it was never going to do what you think it was. If at any time the printer has cleared out the results queue, but the threads are still trying to find more, the queue will think its done and exit. What I replaced it with is another queue to be used as a signal. Each worker puts an item in the queue when its done. Then the printer keeps checking to see if it can pull enough signals from the done queue to equal the amount of workers launched. If so, it will exit.

    This whole thing could still be rewritten better, but I am just applying bandaids to what you have. I sort of just threw this together with what you had.

    Note, the way you start the whole process, checking for the directories under the starting path, will basically just exit out if there are only files.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
Seemingly simple, but I cannot find anything relevant on the web. What is 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.