Write to single file from multiple clients in parfor loop
7 views (last 30 days)
Show older comments
Hello
Im running a parfor loop to run a set of jobs and in each loop I would like to update a my job monitoring file. My job monitoring file is a text file and in each loop I will update it using fopen, fprintf, flclose. I am aware that multiple writes to the same file from different clients may result in a corrupt file quickly. Now I was wondering if there is a method to block other clients while the file is opened.
In 2012 there was a thread concerning the same issue. At this time there were no such options, I hope there has been an update: https://ch.mathworks.com/matlabcentral/answers/33127-whats-the-best-command-to-write-to-file-inside-parfor-loop
If there is no solution to this problem, what might be the best way to write i job monitoring file?
Kind regards
Ben
0 Comments
Answers (1)
Walter Roberson
on 1 Dec 2016
If you open a file with 'a+' permission then each individual fprintf or fwrite would be done atomically at the end of file. Or at least that is the expected behavior on osx and Linux; I do not know if MS Windows atomically has the same behavior.
To use this to write a bunch of data you should assemble the data as a byte array or character vector with embedded newlines and only fwrite it when it is all assembled.
You could also consider sending the data by tcp to a server that is doing the logging.
Depending on the nature of your log file it might be the case that you can take advantage of your system event logging facilities and log file viewer.
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!