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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:56:11+00:00 2026-05-16T11:56:11+00:00

I want to read and write python-source-files from the file system in a thread-safe

  • 0

I want to read and write python-source-files from the file system in a thread-safe way.

open("n2.py","w").write(my_new_py_class)
from myproject import n2
#do something with n2

I assume that this is not thread-safe, since a request2 could modify the file before request1 is loading and executing it. I would like to achieve something like that one thread is waiting till the other thread wrote, loaded, executed and closed the file.

  • 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-16T11:56:12+00:00Added an answer on May 16, 2026 at 11:56 am

    Why are you making your application modify its own files? This is not only incredibly evil, metaprogramming is orders of magnitude harder to understand debug. Plus, python caches modules it imports, so it’s not really easy to reload that. And, last but not least, you don’t have to writ the code to a file to execute it, if you really must execute dynamically generated code.

    To answer your question about writing files in a thread safe way, the general convention is to:

    • Write your content to a temporary file on the same filesystem as your target file.
    • Rename that temporary file to your target file, overwritting it in the process.

    This works, because rename is atomic on POSIX systems, when done on the same device. So other threads/processes will either still have the old file opened, which is now detached from the filesystem and will be deleted as soon as those threads/processes are done with it, or they will open the new file with all of its contents. You avoid having a file that is only half-written.

    In practice, I like to make a temporary directory with python’s tempfile module, and write the file in there, then move it and remove the directory — this way the file is being created with default umask.

    Last but not least, rename is not really atomic on Windows, at least with default settings, as it won’t let you overwrite your old file — you need to do two renames, and that introduces a possibility of race condition. I don’t know a good solution for Windows.

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

Sidebar

Related Questions

I want to: open a file in read-write mode if it exists; create it
I write a lot of Python code where I just want to read a
i want simultaneously read and write data into file. Can i use StreamReader and
Basically I want to read and write to a file then save all the
I want to read a file (hello.in) and to write it to another file
I want to write a batch file to read an input text file, extract
I want to read mails from a mbox file and do some action based
I am trying to write a Python script that will copy files from one
I'm looking to write a Python import filter or preprocessor for source files that
I'm trying to generate customized xml files from a template xml file in python.

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.