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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:01:41+00:00 2026-06-11T16:01:41+00:00

Just started python the other day, new to the entire concept of multi-threading. I

  • 0

Just started python the other day, new to the entire concept of multi-threading. I am having trouble with writing to a file while multi-threading. If I do it the regular way, it will constantly overwrite what’s being written.

What is the proper way to write to a file while using say 5 threads?

  • 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-11T16:01:42+00:00Added an answer on June 11, 2026 at 4:01 pm

    The best way as to not reduce performance is to use a queue between all threads, each thread would enque an item while a main thread would simply deque an item and write it to a file, the queue is thread safe and blocking when its empty, or better yet if possible, simply return all the values from the 5 threads and then write it to the file, IO tends to be one of the more expensive operations we can do, so its best to limit it as much as we can.

    Also note that threads in python don’t take advantage of multiple core, cause of the GIL instead use multiprocessing if you want to leverage multiple processing engines.

    here is a quick example:

    from multiprocessing import Process, Queue
    
    def test_1(q):
        for i in range(10):
            q.put('test_1: ' + str(i))
    
    def test_2(q):
        for i in range(10):
            q.put('test_2: ' + str(i))
    
    q = Queue()
    p1 = Process(target=test_1, args=(q,))
    p2 = Process(target=test_2, args=(q,))
    p1.start()
    p2.start()
    
    with open('test.txt', 'w') as file:
        while p1.is_alive() or p2.is_alive() or not q.empty():
            try:
                value = q.get(timeout = 1)
                file.write(value + '\n')
            except Exception as qe:
                print "Empty Queue or dead process"
    p1.join()
    p2.join()
    

    and the contents of test.txt:

    test_1: 0
    test_1: 1
    test_1: 2
    test_1: 3
    test_1: 4
    test_2: 0
    test_1: 5
    test_2: 1
    test_1: 6
    test_2: 2
    test_1: 7
    test_2: 3
    test_1: 8
    test_2: 4
    test_1: 9
    test_2: 5
    test_2: 6
    test_2: 7
    test_2: 8
    test_2: 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started learning python version 3 and trying to create a file
I just started using/learning Python and have some questions. I have a text file
I just started Python programming, and I'm wondering about the elif keyword. Other programming
I'm new to both Django and Python.. I just started using them a few
I'm very new to python (just started yesterday), and I'm trying to create permutations
So ive just started learning python on WAMP, ive got the results of a
I just started out with Python and wanted to try out tornado. Running the
Hi thanks in advance for your help. I'm just getting started learning Python and
I'm a Python beginner and have just started using packages. When you're calling a
I've just started learning about Gimp scripting using Python, and was wondering, how do

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.