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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:01:02+00:00 2026-05-14T03:01:02+00:00

The tags might not be accurate since I am not sure where the problem

  • 0

The tags might not be accurate since I am not sure where the problem is.

I have a module where I am trying to read some data from a socket, and write the results into a file (append) It looks something like this, (only relevant parts included)

if __name__ == "__main__":
    <some init code>
    for line in file:
        t = Thread(target=foo, args=(line,))
        t.start()
    while nThreads > 0: 
        time.sleep(1)

Here are the other modules,

def foo(text):
    global countLock, nThreads
    countLock.acquire()
    nThreads += 1
    countLock.release()

    """connect to socket, send data, read response"""
    writeResults(text, result)

    countLock.acquire()
    nThreads -= 1
    countLock.release()

def writeResults(text, result):
    """acquire file lock"""
    """append to file"""
    """release file lock"""

Now here’s the problem. Initially, I had a typo in the function ‘foo’, where I was passing the variable ‘line’ to writeResults instead of ‘text’. ‘line’ is not defined in the function foo, it’s defined in the main block, so I should have seen an error, but instead, it worked fine, except that the data was appended to the file multiple times, instead of being written just once, which is the required behavior, which I got when I fixed the typo.

My question is,

1) Why didn’t I get an error?

2) Why was the writeResults function being called multiple times?

  • 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-05-14T03:01:02+00:00Added an answer on May 14, 2026 at 3:01 am

    When you had (simplifying to the important part)

    def foo(text):
        writeResults(line, result)
    

    foo, not having a local variable line, was using the global variable by that name… which happens to be the one set (in the main thread) by for line in file:.

    Specifically, I would expect the total number of lines written to be OK: there’s one thread per line (a weird architecture, BTW) and each thread writes one line… the only issue is, which line each thread writes.

    In your intention, the first thread writes the first line, the second thread writes the second line, etc; but in reality each thread will write the line that happens to be bound to the global line name at the crucial moment when the thread calls writeResults. So, some lines may well end up written multiple times, others, not written.

    For example, suppose the main thread ran faster by enough to start all subthreads before any of them actually gets to writing. In that case, the last value taken by the global name line (i.e., the last line in the file) would be the one written by all of the threads.

    Note that even in the “corrected” version there is no guarantee about the order in which the various lines get written, which is part of what makes this architecture weird — normally, since lines come in a certain order, you’d want to preserve that order on output. I guess your application case is peculiar enough to not need that constraint, but I’m still puzzled that you need so many threads when you’re reading from one file and writing to one file!-)

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

Sidebar

Related Questions

I'm trying to identify an empty element that might or might not contain a
I might have to make some Java classes that dynamically create strings with HTML
I'm not sure if this is even possible, but I have a case where
I'm trying to write a perl script that removes whitespace from XML tags, but
I have read a good bit on the limitations of sending html email from
CSS is not my forte so this might be something simple. I have set
Not sure how to word the question concisely :). I have say 20 Posts
strip_tags only catches tags that have a beginning and end tag. With the strings
Some Tags give me translated Words without setting up the *.po file. {% trans
I have the following Tags in a WSDL : <xs:element name=promoCodeValues maxOccurs=1 minOccurs=0 >

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.