Is reading files in Python updated in real time?

Asked

Viewed 256 times

0

I am developing an application where I will create a multithreading that will read a file .json and based on a key from this file (status: 'pending' or status: 'completed') I will perform an action or not.

The idea is that this action will not be executed twice in the same item, so when I finish the action in an item the status will be changed from pending for completed. My question is, as the file will be read by several instances of Python at the same time, I can trust that the key will be updated in real time, or I need to reopen the file every time I check if it is necessary to touch that item again?

  • 1

    From what you are describing you will have to ensure this in the code, the language does nothing on its own.

1 answer

2


The short answer is: you need to reopen the file and read its contents again. Whenever a file is read - not only in Python, but in any language on conventional operating systems, the system makes a copy of the file data to the data structures in memory.

On the other hand, if you’re doing everything in the same program, you don’t need to use files to flag complete tasks between one thread and another - you can use an object of the type queue for communication between threads - or even variables at module level (global) - depending on your architecture. Queues are more robust, and the race conditions proof, however: https://docs.python.org/2/library/queue.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.