Is reading files in Python updated in real time?

0

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

The idea is that this action is not executed twice on the same item, so when I finish the action on an item the status will be changed from pending to completed . My question is, since the file will be read by multiple instances of Python at the same time, can I trust that the key will be updated in real time, or do I need to reopen the file every time it checks to see if that item needs to be tampered with again?

    
asked by anonymous 10.02.2016 / 12:36

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 in-memory data structures. On the other hand, if you are doing everything in the same program, you do not need to use files to flag complete tasks between one thread and another - you can use an object of type queue for communication between threads - or even variables at the level of module (global) - depending on your architecture. Queues are more robust, and proof of race conditions, however: link

    
13.02.2016 / 07:51