Would a mutex of some sort around each I2C transaction (i.e. complete
A2D reading) be a better way to go?
But why overengineer? If you feel comfortable with the file
solution, go for it! The only drawback might be that it's a
bit slower than other approaches.
I have a Raspberry Pi in my boat that uses I2C to read a number of
voltages and currents (using ADS1115 A2D) so I can monitor the battery condition etc.
At present various different scripts (i.e. processes) just read the
values using the I2C bus whenever they need to but I'm pretty sure
this (quite rarely) results in false readings because two processes
try to read at the same time.
Thus I'm looking for ways to prevent simultaneous access.
One fairly obvious way is to have single process/script which reads
the A2D values continuously and writes them to a file. All other
scripts then read from the file as needed, a simple file lock can then
be used to prevent simultaneous access (well, simultaneous access when
the writing process is writing).
Is this the simplest approach? Are there better ways using
multiprocess? (They look more complicated though).
The I2C bus itself has a mutex but I don't think this guarantees that
(for example) an A2D reading is atomic because one reading takes more
than one I2C bus access.
Would a mutex of some sort around each I2C transaction (i.e. complete
A2D reading) be a better way to go?
One fairly obvious way is to have single process/script which reads the
A2D values continuously and writes them to a file. All other scripts
then read from the file as needed, a simple file lock can then be used
to prevent simultaneous access (well, simultaneous access when the
writing process is writing).
On 7 Jul 2024, at 22:13, Chris Green via Python-list <python-list@python.org> wrote:
a simple file lock can then
be used to prevent simultaneous access (well, simultaneous access when
the writing process is writing).
On 7 Jul 2024, at 22:13, Chris Green via Python-list <python-list@python.org> wrote:
a simple file lock can then
be used to prevent simultaneous access (well, simultaneous access when
the writing process is writing).
There is a simple pattern to make this robust.
Write new values to a tmp file.
Close the tmp file.
Then use os.rename(tmpfile, productionfile).
This is guaranteed that any process that reads the file will only see all the old file contents or all the new file contents, never a mix of both.
On 7 Jul 2024, at 23:47, MRAB via Python-list <python-list@python.org> wrote:
For clarity I'd recommend os.replace instead. This is because on Windows os.rename it would complain if the target file already exists, but os.replace has the same behaviour on both Linux and Windows.
On Sat, 6 Jul 2024 08:28:41 +0100, Chris Green wrote:
One fairly obvious way is to have single process/script which reads the
A2D values continuously and writes them to a file. All other scripts
then read from the file as needed, a simple file lock can then be used
to prevent simultaneous access (well, simultaneous access when the
writing process is writing).
The thing with a file is, it persists even when the collector process is
not running. Do you want data that persists when the collector process is
not running?
Is this a history of values, or just a snapshot of current values? A
history of values could be written to a database. Databases provide their
own transactions and interlocking to prevent readers from reading partial updates.
If it’s a snapshot of current values, that does not persist when the collector process is not running, then why not just keep the data in the memory of the collector process, and have it concurrently listen on a
socket for connections from readers requesting a copy of the current data?
On 06/07/2024 09.28, Chris Green wrote:
I have a Raspberry Pi in my boat that uses I2C to read a number of
voltages and currents (using ADS1115 A2D) so I can monitor the battery condition etc.
At present various different scripts (i.e. processes) just read the
values using the I2C bus whenever they need to but I'm pretty sure
this (quite rarely) results in false readings because two processes
try to read at the same time.
Thus I'm looking for ways to prevent simultaneous access.
Why using "different scripts"?
Is it there any particular reason?
Maybe it would be better, if possible, to have
a single script, which, sequentially, reads
whatever needs to be read (or written).
In a loop.
This is even simpler than using a file.
That's exactly the sort of solution I was wondering about. Is there a
ready made module/library for handling this sort of thing? Basically
it will just be a string of a few tens of characters that would be
kept up to date by one process and asked for by all the others.
Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
If it’s a snapshot of current values, that does not persist when the
collector process is not running, then why not just keep the data in
the memory of the collector process, and have it concurrently listen on
a socket for connections from readers requesting a copy of the current
data?
That's exactly the sort of solution I was wondering about. Is there a
ready made module/library for handling this sort of thing? Basically it
will just be a string of a few tens of characters that would be kept up
to date by one process and asked for by all the others.
Chris Green <cl@isbd.net> wrote or quoted:
That's exactly the sort of solution I was wondering about. Is there a >ready made module/library for handling this sort of thing? Basically
it will just be a string of a few tens of characters that would be
kept up to date by one process and asked for by all the others.
I'm not an expert here, and just quickly tried to make it
run, so the code will still contain errors and not contain
something necessary, but might give you a starting point.
A process doing something (here: printing an incrementing value
named "info") and also serving requests from other processes
for this "info" value:
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 37:09:34 |
Calls: | 10,392 |
Calls today: | 3 |
Files: | 14,064 |
Messages: | 6,417,161 |