Confused about updating common files with Pythonista script on iphone and ipad
tbrown313 last edited by ccc
I have written a Pythonista script using the ui to create and update multiple files that are stored in the Pythonista directory of iCloud. I run the script using a shortcut and I run it from both my iPhone and my iPad.
The problem is that when going back and forth between the iPhone and iPad accessing and modifying the same files, sometimes the files are not synchronized.
In the Pythonista file browser, the files, and the script are in iCloud/Pythonista3/Books directory.
For one file where I get this type of error, I open the file every time the program is run using:
myfile = open ('CurrentBooks.txt').read() cBooks = json.loads(myfile)
The file is not explicitly closed when it is only read.
When I change the file, then I do the following:
with open('CurrentBooks.txt', 'w') as outfile: json.dump(mydict, outfile) outfile.close()
But I have found that, sometimes, when I add an entry to this file using the iPad, when I access the file on the iPhone, the changes are not there.
What is going on? Is this an iCloud problem? Or am I doing something wrong in the code?
Also, I wonder if this type of operation can get confused if the gui portion of the app is open at the same time on both devices. Since there appears to be no way to close the gui app from Pythonista, I have to remember to touch the 'X' to close the app each time I run it. I am sure that at times I have not done this.
ccc last edited by ccc
The first code block has two problems.
- The file is being opened but never closed.
- The indentation on the second line is neither needed nor accepted because the line above it does not end with a semicolon.
The second code block is the best practice because
with open() aswill automatically close the file at the exit of
withblock . So the last line
outfile.close()is redundant and can be safely removed.
http://omz-software.com/pythonista/docs/ios/ui.html#about-actions-and-delegates contains a discussion of the
@ui.in_backgroundfunction decorator which might also be of interest.
mikael last edited by
@tbrown313, my experience has led me to not rely on iCloud for any kind of ”real-time” back-and-forth synchronization of files.
Whether the UI is open or not has no impact on this. When you write within the
withblock the file is reliably updated unless your code never reaches the block, there is an exception, or you write huge amounts of data and kill Pythonista in the middle of the write.
You might do better with any service where you explicitly sync the files with a central server and can check the status, e.g. Github, OneDrive, Google Drive or similar.
tbrown313 last edited by
@ccc: Thanks for the comment. FYI, the identation in the first code block is only in this post and resulted from a copy and paste error.
Thanks for the detail on
with open() as
I'm an old hardware designer who wrote a lot of internal diagnostics code that was never for production and the depth of my knowledge of such details is scant. This helps me.
Unfortunately, I think you are correct about iCloud, and it is very disappointing. It is interesting that I have had good luck with some apps that use iCloud--like Drafts, for example--that seem much more reliable than Apple's efforts--like Notes, for example. I am going to play with storing my data in Drafts to see it that works more reliably.
Also, as a workaround when I notice a missed update failure, I found this on the forum:
The last comment suggests a technique to force an iCloud update in Pythonista and I have had a bit of luck with that.
Maybe this is a question for a separate thread, but what is the effect of running a gui pythonista app multiple times from a shortcut without closing the Pythonista gui app? If I do this (my gui is less that full screen size), then the Pythonista gui app windows just stack up. Doesn't seem like a good thing, but if I handle the opening/closing of files properly, is this a problem? Of course, I wouldn't want a few hundred of them stacked up--but I would notice eventually and close them :).