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

  • Home
  • SEARCH
  • 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 8324813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:12:47+00:00 2026-06-09T00:12:47+00:00

I have a subroutine of a Python application that takes a list of eight-character

  • 0

I have a subroutine of a Python application that takes a list of eight-character serial numbers and searches a set of directories for filenames containing those SNs with a range of possible extensions. The function that does it is as follows.

def CompleteSN(SNorig, directories, exts, directoryContents=None):

    # Split SNorig into its directory path and basename
    directory, basename = os.path.split(SNorig)

    #Don't try to complete if it has a directory already
    if len(directory) > 0:
        return SNorig

    # Split the basename into extension (ext) and serial number (SN)
    SN = basename[:8].upper()

    #If we have a cache, check it
    if directoryContents is not None:
        if SN in directoryContents:
            return directoryContents[SN]
        else:
            return None

    possSNs = ['.'.join((SN, ext)) for ext in exts]

    for pSN in possSNs:
        for d in directories:
            dpath = os.path.join(d, pSN)
            if os.path.exists(dpath):
                return dpath

    return None

SN is the serial number to turn into a complete path, directories is a list of directories to look for it in, exts is the list of possible extensions to try (with no leading dots), and directoryContents is either None or a large (hundreds of thousands of entries) dict mapping the serial numbers of files in the directories to search to their full names. My thought was that for large sets of serial numbers to complete, it would be faster to put all the serial numbers that could be found into a dictionary so they could be quickly looked up rather than making a system call for each one. For large numbers of SNs to complete, this would be worth the cost of making the dict.

And it is much faster once directoryContents is populated, as I predicted. However, this is only the case when not using multiprocessing. I allow the user to toggle multiprocessing mode, which is faster as expected when directoryContents is None. (i.e. when we’re finding the files by checking their existence with os.path.exists) However, when checking the dict for them, calling CompleteSN in a multiprocessing.Pool is bizarrely slow, on the order of fewer than 10 SNs/second. (Compared with thousands when not using multiprocessing) My best guess as to what is going on is that whatever mechanism Python uses to share data between pool workers (of which I think there are 8) is getting bogged down with such a large dict structure. Can anyone help me learn what is going on here?

  • 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-09T00:12:49+00:00Added an answer on June 9, 2026 at 12:12 am

    In general, you shouldn’t share data structures between multiple processes. The overhead in synchronization often kills performance. Perhaps you can create individual copies of your look-up dict, one for each process?

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

Sidebar

Related Questions

I have a subroutine that changes its operation slightly to include either one list
I have a subroutine that searches for an occurrence of a string in another
I have a subroutine that takes a filehandle as an argument. How do I
I have a simple subroutine that loads a list from a database. I would
Let's say I have a subroutine/method that a user can call to test some
I have code that calls an ENTRY in a SUBROUTINE before the SUBROUTINE .
I have this Perl subroutine that is causing a problem: sub new { my
Here is what I have as VBScript Subroutine: sub buildChildAdminStringHierarchical(byval pAdminID, byref adminString) set
I have a subroutine in Perl that should be indented like this: sub GetFiles
I have a subroutine that returns a reference to a hash. I want to

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.