Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Best of all is never to have been born. Second best is to die soon.


comp / comp.lang.python / Re: Best (simplest) way to share data between processes

SubjectAuthor
* Best (simplest) way to share data between processesChris Green
+* Re: Best (simplest) way to share data between processesStefan Ram
|`- Re: Best (simplest) way to share data between processesGordinator
+* Re: Best (simplest) way to share data between processesPiergiorgio Sartor
|`- Re: Best (simplest) way to share data between processesChris Green
+* Re: Best (simplest) way to share data (Posting On Python-List Prohibited)Lawrence D'Oliveiro
|`* Re: Best (simplest) way to share data (Posting On Python-List Prohibited)Chris Green
| +* Re: Best (simplest) way to share dataStefan Ram
| |`- Re: Best (simplest) way to share dataChris Green
| `- Re: Best (simplest) way to share data (Posting On Python-List Prohibited)Lawrence D'Oliveiro
+- Re: Best (simplest) way to share data between processesBarry
+- Re: Best (simplest) way to share data between processesMRAB
+- Re: Best (simplest) way to share data between processesBarry Scott
`- Re: Best (simplest) way to share data between processesLeft Right

1
Subject: Best (simplest) way to share data between processes
From: Chris Green
Newsgroups: comp.lang.python
Date: Sat, 6 Jul 2024 07:28 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Best (simplest) way to share data between processes
Date: Sat, 6 Jul 2024 08:28:41 +0100
Lines: 30
Message-ID: <9a8nlk-jb81.ln1@q957.zbmc.eu>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net qLT1SZjzbZyQrUo5d68r1wEM0IbpM+9mH4fgX/p7jNCOa/P8s=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:bFoeaz/sDZyEuj7lyhkauVR6xvE= sha256:ILneqJLXf7Qb7UVRb261QxsYXXt2iHtAbD3dBN6LVl8=
User-Agent: tin/2.6.4-20240224 ("Banff") (Linux/6.8.0-36-generic (x86_64))
View all headers

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?

--
Chris Green
·

Subject: Re: Best (simplest) way to share data between processes
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Sat, 6 Jul 2024 11:32 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data between processes
Date: 6 Jul 2024 11:32:53 GMT
Organization: Stefan Ram
Lines: 17
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <mutex-20240706123143@ram.dialup.fu-berlin.de>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de BwGtF8s0VQQtok3U3Crh9QZrwnBGAOGhTABlJ6+UaQiutR
Cancel-Lock: sha1:mEzMA1LR3Qzhup045BDbQ3Ta2zg= sha256:aBEBqyivcHr0gIjONwycerciB+NgBq1qlSd8TjblngY=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

Chris Green <cl@isbd.net> wrote or quoted:
>Would a mutex of some sort around each I2C transaction (i.e. complete
>A2D reading) be a better way to go?

I not an expert here, but from what I heard, you could use
a named POSIX semaphore as a kind of system-wide mutex.

Or, the single process reading the A2D values could write
them to a shared memory segment the access to which is
controlled by a mutex.

Or, you could use a message queue (like POSIX mq) to
distribute readings.

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.

Subject: Re: Best (simplest) way to share data between processes
From: Gordinator
Newsgroups: comp.lang.python
Organization: usenet-news.net
Date: Sat, 6 Jul 2024 19:03 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-2.nntp.ord.giganews.com!border-4.nntp.ord.giganews.com!nntp.giganews.com!npeer.as286.net!npeer-ng0.as286.net!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx05.ams4.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Best (simplest) way to share data between processes
Newsgroups: comp.lang.python
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
<mutex-20240706123143@ram.dialup.fu-berlin.de>
Content-Language: en-US
From: gordinator@gordinator.org (Gordinator)
In-Reply-To: <mutex-20240706123143@ram.dialup.fu-berlin.de>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 15
Message-ID: <TxgiO.202586$%wo8.26426@fx05.ams4>
X-Complaints-To: abuse@usenet-news.net
NNTP-Posting-Date: Sat, 06 Jul 2024 19:03:15 UTC
Organization: usenet-news.net
Date: Sat, 6 Jul 2024 20:03:15 +0100
X-Received-Bytes: 1413
X-Original-Bytes: 1271
View all headers

On 06/07/2024 12:32, Stefan Ram wrote:
> 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 absolutely agree. Overengineering is generally a bad idea because
you're using a complex solution to solve a simple problem, which leads
to unexpected breakage.

The file solution is perfectly fine, and file locks are used by really
important software (i.e package managers and such) because its
simplicity makes it almost never fail.

So yeah, go for it!

Subject: Re: Best (simplest) way to share data between processes
From: Piergiorgio Sartor
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Sat, 6 Jul 2024 19:32 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de (Piergiorgio Sartor)
<piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de>
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data between processes
Date: Sat, 6 Jul 2024 21:32:44 +0200
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <sniolk-8t7.ln1@lazy.lzy>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 06 Jul 2024 21:33:57 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7be1f0409d9be439c6bb3cc1b7bb2cf6";
logging-data="4144129"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yWVrZ2Bl18/iWwak7Qwtj"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:jDY31jpnCO1yiiV9ONLokfx1ioY=
In-Reply-To: <9a8nlk-jb81.ln1@q957.zbmc.eu>
Content-Language: it, en-GB
View all headers

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.

bye,

pg

> 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?
>

--

piergiorgio

Subject: Re: Best (simplest) way to share data (Posting On Python-List Prohibited)
From: Lawrence D'Oliv
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Sun, 7 Jul 2024 01:43 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data (Posting On Python-List
Prohibited)
Date: Sun, 7 Jul 2024 01:43:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <v6crrk$1kfb$6@dont-email.me>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 07 Jul 2024 03:43:16 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="68b1498c9728a898ae95fa9c50f05ec1";
logging-data="53739"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/cre+Uemp8JZX2RH8PrTs"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:uDh/0X3l7UfzHfSwStBADEesPOU=
View all headers

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?

Subject: Re: Best (simplest) way to share data between processes
From: Barry
Newsgroups: comp.lang.python
Date: Sun, 7 Jul 2024 22:27 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: barry@barrys-emacs.org (Barry)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data between processes
Date: Sun, 7 Jul 2024 23:27:01 +0100
Lines: 21
Message-ID: <mailman.14.1720391247.2981.python-list@python.org>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
<B4D7CB24-28E5-4CAC-AA42-5C47C3F6F470@barrys-emacs.org>
Mime-Version: 1.0 (1.0)
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 5QhEIUr5IcgBYpLarHrsNgEFJJWhgSQdgeF10xfT9xgQ==
Cancel-Lock: sha1:dfX5Reu9fgnzEHLEoPi6soYE36M= sha256:JpLruSNcVZedUqvn89WhURDw6IFpVcangx1SrzXDuUA=
Return-Path: <barry@barrys-emacs.org>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.009
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'jul': 0.04; 'cc:addr
:python-list': 0.09; 'from:addr:barry': 0.09; 'received:217.70':
0.09; 'received:217.70.178': 0.09; 'received:gandi.net': 0.09;
'received:mail.gandi.net': 0.09; 'subject:between': 0.09; 'cc:no
real name:2**0': 0.14; 'barry': 0.16; 'both.': 0.16; 'from:addr
:barrys-emacs.org': 0.16; 'message-id:@barrys-emacs.org': 0.16;
'subject:way': 0.16; 'wrote:': 0.16; 'values': 0.17;
'cc:addr:python.org': 0.20; 'cc:2**0': 0.25; 'pattern': 0.26;
'old': 0.27; 'chris': 0.28; 'python-list': 0.32; 'there': 0.33;
'header:In-Reply-To:1': 0.34; 'close': 0.35; 'file': 0.38; 'use':
0.39; 'skip:o 10': 0.61; 'lock': 0.64; 'guaranteed': 0.67;
'prevent': 0.67; 'received:217': 0.67; 'mix': 0.69; 'contents,':
0.84; 'simultaneous': 0.84; 'tmp': 0.84; 'green': 0.96
In-Reply-To: <9a8nlk-jb81.ln1@q957.zbmc.eu>
X-Mailer: iPad Mail (21F90)
X-GND-Sasl: barry@barrys-emacs.org
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <B4D7CB24-28E5-4CAC-AA42-5C47C3F6F470@barrys-emacs.org>
X-Mailman-Original-References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
View all headers

> 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.

Barry

Subject: Re: Best (simplest) way to share data between processes
From: MRAB
Newsgroups: comp.lang.python
Date: Sun, 7 Jul 2024 22:47 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: python@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data between processes
Date: Sun, 7 Jul 2024 23:47:01 +0100
Lines: 20
Message-ID: <mailman.15.1720392424.2981.python-list@python.org>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
<B4D7CB24-28E5-4CAC-AA42-5C47C3F6F470@barrys-emacs.org>
<6e86fff1-d24c-42c9-bc72-7e22757686ec@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de iaoOKKQhnD8j8Z4ST+GLWw8+wNT/TlrAIkQB45Skc05g==
Cancel-Lock: sha1:GQTrPy2svG1dF5ewfPsnZsK6sQU= sha256:2duPQ3HL4k1luvhCDffPMOJ240jhmNhuS85UDTS8OjQ=
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=Ny/W/QCn;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.009
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'jul': 0.04;
'from:addr:python': 0.09; 'linux': 0.09; 'received:192.168.1.64':
0.09; 'subject:between': 0.09; 'barry': 0.16; 'behaviour': 0.16;
'both.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16;
'from:name:mrab': 0.16; 'instead.': 0.16; 'message-
id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16;
'received:84.93.230': 0.16; 'received:plus.net': 0.16;
'subject:way': 0.16; 'windows.': 0.16; 'wrote:': 0.16; 'values':
0.17; 'to:addr:python-list': 0.20; "i'd": 0.24; 'pattern': 0.26;
'old': 0.27; 'chris': 0.28; 'header:User-Agent:1': 0.30; 'python-
list': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'there':
0.33; 'windows': 0.34; 'same': 0.34; 'header:In-Reply-To:1': 0.34;
'close': 0.35; 'target': 0.36; 'received:192.168': 0.37; 'file':
0.38; 'use': 0.39; 'both': 0.40; 'skip:o 10': 0.61; 'lock': 0.64;
'guaranteed': 0.67; 'prevent': 0.67; 'clarity': 0.69; 'mix': 0.69;
'contents,': 0.84; 'simultaneous': 0.84; 'tmp': 0.84; 'green':
0.96
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1720392422; bh=dF4UNapqHZpfXxobNLAUHzvla/rXqLtYRC3WxtMMoCo=;
h=Date:Subject:To:References:From:In-Reply-To;
b=Ny/W/QCn32QccgiGRGETghChymtQspuWRM325og0lKh7sL2Qw+1WE0UcloFFYmfJE
5lUDvT3+FjoS/umEQhRpeeQ9+e93uB2/WDdBVDPC5v1i4GDy2X1+gohVqlL/mnN03I
MaUh6rqcc7iTBMO0H5rnk1UqPRHLWKUglTtHFHtgeCRK1eakpCaewKLCiiIkqj0V+t
bCvXLN/jeE1ssQfEx9mJXU2SudQw1l9yS19ahJK0J9K2WNGKPvoJj4gfAuTeWNIGN7
H3GoVhevYaXDX+HshUTGgsBbKsmOGIImY4IG5m13e1xukf8chRcnX+d/ip6UAc87QX
RPu/9TOEvvjLQ==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=AfoSHGXG c=1 sm=1 tr=0 ts=668b1ae6
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=8AHkEIZyAAAA:8 a=D8jjOKPysQHQejHcGhQA:9 a=QEXdDO2ut3YA:10
a=zZCYzV9kfG8A:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <B4D7CB24-28E5-4CAC-AA42-5C47C3F6F470@barrys-emacs.org>
X-CMAE-Envelope: MS4xfKNAfY4Y2WI4dBK4ygthiUoZYrILj8qdtfXu3u5CuzDcHn2hEXPe288HpbTFV/DtTefKtw9XyXxe+LKhdFPsv89761vWPDBLRt/yQ5p7+6FDxZB9Y7F1
XMX95ofdQ3PHL/vT3W66al2bWd35uflAAKGR9fpetU2YOfktmTEn16BNaDMSYnvO1ANvq06/2Sl65ataDtWS8lgD8GHsbixnsac=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <6e86fff1-d24c-42c9-bc72-7e22757686ec@mrabarnett.plus.com>
X-Mailman-Original-References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
<B4D7CB24-28E5-4CAC-AA42-5C47C3F6F470@barrys-emacs.org>
View all headers

On 2024-07-07 23:27, Barry via Python-list wrote:
>
>
>> 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.
>
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.

Subject: Re: Best (simplest) way to share data between processes
From: Barry Scott
Newsgroups: comp.lang.python
Date: Mon, 8 Jul 2024 08:34 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: barry@barrys-emacs.org (Barry Scott)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data between processes
Date: Mon, 8 Jul 2024 09:34:44 +0100
Lines: 15
Message-ID: <mailman.20.1720427697.2981.python-list@python.org>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
<B4D7CB24-28E5-4CAC-AA42-5C47C3F6F470@barrys-emacs.org>
<6e86fff1-d24c-42c9-bc72-7e22757686ec@mrabarnett.plus.com>
<E1833CA2-AF81-4499-BB69-666F28D68B2E@barrys-emacs.org>
Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\))
Content-Type: text/plain;
charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de S6eHFGEbU90NiVVjSl1LzgSMXMyvyy3ekI4VgqB6dVjw==
Cancel-Lock: sha1:0h5s/V6IdhU7cS70zSFF2SJwQ3s= sha256:Rie0V2EMOUi3SwNu1bdK3bWdspjOXcyjfZ6y/hVOPIg=
Return-Path: <barry@barrys-emacs.org>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'jul': 0.04; 'cc:addr
:python-list': 0.09; 'from:addr:barry': 0.09; 'linux': 0.09;
'received:217.70': 0.09; 'received:gandi.net': 0.09;
'received:mail.gandi.net': 0.09; 'subject:between': 0.09; 'cc:no
real name:2**0': 0.14; 'barry': 0.16; 'behaviour': 0.16;
'from:addr:barrys-emacs.org': 0.16; 'from:name:barry scott': 0.16;
'instead.': 0.16; 'message-id:@barrys-emacs.org': 0.16; 'rpi.':
0.16; 'subject:way': 0.16; 'windows.': 0.16; 'wrote:<span': 0.16;
'wrote:': 0.16; 'cc:addr:python.org': 0.20; "i'd": 0.24;
'cc:2**0': 0.25; 'email addr:python.org&gt;': 0.28; 'python-list':
0.32; 'but': 0.32; '0);': 0.33; 'rgb(0,': 0.33; 'windows': 0.34;
'same': 0.34; 'header:In-Reply-To:1': 0.34; 'target': 0.36;
'file': 0.38; 'both': 0.40; 'font-family:': 0.60; 'inline': 0.61;
'display:': 0.62; 'none;': 0.62; 'font-weight:': 0.62; '0px;':
0.63; 'text-indent:': 0.64; 'normal;': 0.66; 'received:217': 0.67;
'skip:! 10': 0.67; 'clarity': 0.69; 'float:': 0.74; 'email
name:&lt;python-list': 0.84; '400;': 0.89; '13px;': 0.91
In-Reply-To: <6e86fff1-d24c-42c9-bc72-7e22757686ec@mrabarnett.plus.com>
X-Mailer: Apple Mail (2.3774.600.62)
X-GND-Sasl: barry@barrys-emacs.org
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <E1833CA2-AF81-4499-BB69-666F28D68B2E@barrys-emacs.org>
X-Mailman-Original-References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
<B4D7CB24-28E5-4CAC-AA42-5C47C3F6F470@barrys-emacs.org>
<6e86fff1-d24c-42c9-bc72-7e22757686ec@mrabarnett.plus.com>
View all headers

> 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.

Agreed.

In this case the OP is on an RPi.

Barry

Subject: Re: Best (simplest) way to share data between processes
From: Left Right
Newsgroups: comp.lang.python
Date: Sun, 7 Jul 2024 21:55 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: olegsivokon@gmail.com (Left Right)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data between processes
Date: Sun, 7 Jul 2024 23:55:09 +0200
Lines: 65
Message-ID: <mailman.22.1720443071.2981.python-list@python.org>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
<CAJQBtgn3t+ADURKODqMc3BDoK9v_1hP098AXxi0_JeLqWSAAHg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de GAG+BT1mEWop8rUORP9EBQPtYBPfK0pnqbDWz7Qfkn8g==
Cancel-Lock: sha1:k0bi5e2EGfmF2e+S3hx42xx7Zzk= sha256:cDWinOQVMOtO7rBi67s9uNH+9BSjXBqrlnMIUQkpPJY=
Return-Path: <olegsivokon@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=HkHIvx95;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'jul': 0.04; '(for': 0.05;
'fairly': 0.05; "python's": 0.05; 'usage': 0.05; 'http': 0.07;
'queue': 0.07; 'sun,': 0.07; 'approach?': 0.09; 'fact,': 0.09;
'linux': 0.09; 'raspberry': 0.09; 'solution,': 0.09;
'subject:between': 0.09; 'url:mailman': 0.15; '2024': 0.16; 'bus':
0.16; 'directly,': 0.16; 'guarantees': 0.16; 'script,': 0.16;
'subject:way': 0.16; 'utilities': 0.16; 'writes': 0.16; 'wrote:':
0.16; 'values': 0.17; 'instead': 0.17; 'uses': 0.19; 'server.':
0.19; 'to:addr:python-list': 0.20; 'run': 0.23; 'idea': 0.24;
'url-ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'url-ip:188.166/16': 0.25; 'again,': 0.26;
'do,': 0.26; "isn't": 0.27; 'chris': 0.28; 'requests': 0.28;
'present': 0.30; 'takes': 0.31; 'putting': 0.31; 'url-ip:188/8':
0.31; 'program': 0.31; 'think': 0.32; 'needed,': 0.32; 'python-
list': 0.32; 'message-id:@mail.gmail.com': 0.32; 'but': 0.32;
"i'm": 0.33; 'there': 0.33; 'server': 0.33; 'same': 0.34;
'package': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'running': 0.34; 'one.': 0.35;
'runs': 0.35; 'from:addr:gmail.com': 0.35; 'processes': 0.36;
'change': 0.36; 'using': 0.37; 'file': 0.38; 'way': 0.38; 'could':
0.38; 'read': 0.38; 'two': 0.39; 'single': 0.39; 'use': 0.39;
'rest': 0.39; 'still': 0.40; 'whenever': 0.40; 'want': 0.40;
'try': 0.40; 'simply': 0.63; 'feel': 0.63; 'complete': 0.64;
'lock': 0.64; 'thus': 0.64; 'transaction': 0.64; 'your': 0.64;
'url-ip:45/8': 0.65; 'look': 0.65; 'well': 0.65; 'time.': 0.66;
'prevent': 0.67; 'obvious': 0.69; 'care': 0.71; "you'll": 0.73;
'easy': 0.74; 'free.': 0.76; 'monitor': 0.81; 'boat': 0.84;
'executing': 0.84; 'orders': 0.84; 'simultaneous': 0.84;
'battery': 0.93; 'green': 0.96
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1720389320; x=1720994120; darn=python.org;
h=content-transfer-encoding:to:subject:message-id:date:from
:in-reply-to:references:mime-version:from:to:cc:subject:date
:message-id:reply-to;
bh=ayPmrE6qYmVeEl25ntZOH593YhfAaxiJTEhv/anjnX4=;
b=HkHIvx95+nuCXAW+wri4YapBT4C/Nfapp0uxhKz2fH7EsLhiUcnxjhRyoS7Ju6Uaah
ViRthGU5qFoYRmnakyRsuW7ksQWmLsEVN51JHT8AvpPpfqZ1f5iAGaSlzoMslcLLS2xe
hC8aZxOdc5VsCYxLdfBY9Jk7PSPdXg1Ethj48noOl3TpvNV7HBRTxvP1WD48kN4/QaV3
rZsBrAWhyCpQRMD13yy9DDsazTUY1mPo85UglErxd4ymU8RLLGd2Bjw6ecD9f+gVIomQ
mN7qP8EoS4dcnNINzBqNyzgUmG7xXp3ASndLCi3fnwi5HsNcGAVcJMcDfTBGaF2PWFOm
sKqg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1720389320; x=1720994120;
h=content-transfer-encoding:to:subject:message-id:date:from
:in-reply-to:references:mime-version:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to;
bh=ayPmrE6qYmVeEl25ntZOH593YhfAaxiJTEhv/anjnX4=;
b=Smr2dRtcqdwG+4gFkac03vFvDKw2SbsZPwDeZybpblDJVmV3t0gf1+2CXW/IfIUZhi
e/nWrBgUH6HhOCsn3nwVnpSFKw75tHzBMal5FcVx63+slpbbDGSKrG9FD2X5PotVgDgV
bcmOAF8IViWOb24TyuEelioFpjoPErzIivTZEJ6Qk7r6EdMN4UakFao34W3jX6+1QCdR
2+gwDFkcMddqmOT9uMYDX5WN1NIvfoCeC5IntFkn3lxBkpzCOPJRxdFcuo5hGyd03ooZ
Dn+clYtfQNPPDUeb5BlCzIQN4Vcnwxxr6UwpmMa1lQkkXzGwWrziyKjdrXDVFNw/XEKC
YImQ==
X-Gm-Message-State: AOJu0YyD3x24mxb0XmSNlAXiRCZR1LAXOHBvTy/RcavnHYVu5wOLjD5e
0Wkt1+sx+4Cfag6INXm4cUuqSHClnWP7QLDHSW6RQ5sVrDBPVqCO2leHtCfi1jPzj9N2nqK+08K
rGAbwP5+XlEQ5pWzRAwZgjDWm5pNAlLLK
X-Google-Smtp-Source: AGHT+IHUUvaa/PZ3AFdXFbhVo8QdgKnltVQbYf5Rs6Wljl/MiiyNNoB5YyyVpqp7PGTtIwdtmj9dTv9l3YyOMkOqd98=
X-Received: by 2002:a05:622a:138d:b0:440:480a:8b69 with SMTP id
d75a77b69052e-447cbf26054mr120675281cf.28.1720389320312; Sun, 07 Jul 2024
14:55:20 -0700 (PDT)
In-Reply-To: <9a8nlk-jb81.ln1@q957.zbmc.eu>
X-Mailman-Approved-At: Mon, 08 Jul 2024 08:51:10 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAJQBtgn3t+ADURKODqMc3BDoK9v_1hP098AXxi0_JeLqWSAAHg@mail.gmail.com>
X-Mailman-Original-References: <9a8nlk-jb81.ln1@q957.zbmc.eu>
View all headers

If resource usage isn't an issue, then the _easy_ thing to do, that
would also be easily correct is to have a server doing all the
h/w-related reading and clients talking to that server. Use for the
server the technology you feel most confident with. Eg. you may use
Python's http package. I believe that the server from this package
runs in a single thread, and thus processes all requests
synchronously. So, you'll get synchronization for free.

Then, the rest of the scripts that need to talk to h/w will instead be
talking to this server.

Again, this isn't an _efficient_ solution... but, sometimes you don't
need one. And this one is easy to make, easy to debug, easy to expand.
But, if instead you were looking for a more efficient solution, then,
the general idea that allows the http server to work in this case
would still apply: have a single synchronization program that takes
requests asynchronously, and orders them. So, a basic TCP server would
also work as well as a UNIX socket. Your idea with holding a lock on a
file would also work (in fact, plenty of Linux utilities work that
way, eg. apt-get or yum).

----

If you don't want to change the existing script, then instead of
running them directly, you could run them through batch:
https://man7.org/linux/man-pages/man1/batch.1p.html this is a very
simply queuing program that's available for Linux. It will take care
of synchronization by putting the scripts you want to run in a queue
and executing them one at a time.

On Sun, Jul 7, 2024 at 11:12 PM Chris Green via Python-list
<python-list@python.org> 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.
>
> 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?
>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list

Subject: Re: Best (simplest) way to share data (Posting On Python-List Prohibited)
From: Chris Green
Newsgroups: comp.lang.python
Date: Mon, 8 Jul 2024 12:56 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data (Posting On Python-List Prohibited)
Date: Mon, 8 Jul 2024 13:56:34 +0100
Lines: 35
Message-ID: <294tlk-pqr1.ln1@q957.zbmc.eu>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu> <v6crrk$1kfb$6@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net V9JdruBFmkTH2bnPtFfJRgm7pmIDkqNgh70p7hYriNR5gz2Z8=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:o6sEfRY8ucGRE8loGC8CeJwUz3I= sha256:1vFFO02F3ezVEBoMMlMloHPW4xCAIWuvbvNukqADCq0=
User-Agent: tin/2.6.4-20240224 ("Banff") (Linux/6.8.0-36-generic (x86_64))
View all headers

Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
> 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.
>
There's a separate (crontab driven) process that writes the history to
a sqlite3 database,

> 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
·

Subject: Re: Best (simplest) way to share data between processes
From: Chris Green
Newsgroups: comp.lang.python
Date: Mon, 8 Jul 2024 12:52 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data between processes
Date: Mon, 8 Jul 2024 13:52:59 +0100
Lines: 31
Message-ID: <b24tlk-pqr1.ln1@q957.zbmc.eu>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu> <sniolk-8t7.ln1@lazy.lzy>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net Pk/sBjgxt/NmCWttpCGV/ABvaZWFhXD5D0k3gP2eRi8fRDVaM=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:App/QMWtcoN4Wn/BvRLwpillWNM= sha256:j5+2l6GDNGNyaBYW7hTXj8wdLsIt+yN7Tjgttv0VCZQ=
User-Agent: tin/2.6.4-20240224 ("Banff") (Linux/6.8.0-36-generic (x86_64))
View all headers

Piergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.removethis.de> wrote:
> 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.
>
Yes, but it's conceptually (and programming wise) much simpler to have
separate scripts. Some of them are simple 'on demand' scripts that I
run from the command line when I want to know something. Others are
scripts that drive displays on control panels.

--
Chris Green
·

Subject: Re: Best (simplest) way to share data
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Mon, 8 Jul 2024 15:36 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data
Date: 8 Jul 2024 15:36:57 GMT
Organization: Stefan Ram
Lines: 65
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <server-20240708162329@ram.dialup.fu-berlin.de>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu> <v6crrk$1kfb$6@dont-email.me> <294tlk-pqr1.ln1@q957.zbmc.eu>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de EP/XfQQ9F8KRilPfAQov3gE1nzFpcS2K8MulYM2qgWhPO1
Cancel-Lock: sha1:RQOMeO5TYdRU82i5Y2GLHA2UG7A= sha256:L0Iew4kTPsUbseSGpJBh8vG3kXM8vbdISgq+faQhgXc=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

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:

import asyncio
import socket

class Server:
def __init__( self ):
self.info = 0

async def handle_client( self, reader, writer ):
data = await reader.read( 100 )
message = data.decode()
addr = writer.get_extra_info( 'peername' )
print( f"Received {message!r} from {addr}" )

if message.strip() == "getinfo":
writer.write( str( self.info ).encode() )
await writer.drain()

writer.close()
await writer.wait_closed()

async def print_number( self ):
while True:
print( self.info )
self.info += 1
await asyncio.sleep( 1 )

async def serve( self ):
server = await asyncio.start_server( self.handle_client, '127.0.0.1', 8888 )
addr = server.sockets[0].getsockname()
print( f'Serving on {addr}' )

async with server:
await asyncio.gather( server.serve_forever(), self.print_number() )

asyncio.run( Server().serve() )

, and an example client:

import socket
import time

while True:
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.connect( ( '127.0.0.1', 8888 ))
s.send( "getinfo".encode() )
data = s.recv( 100 )
message = data.decode().strip()
print( f"Received: {message}" )
time.sleep( 3 )

.

Subject: Re: Best (simplest) way to share data (Posting On Python-List Prohibited)
From: Lawrence D'Oliv
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Tue, 9 Jul 2024 05:52 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data (Posting On Python-List
Prohibited)
Date: Tue, 9 Jul 2024 05:52:53 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <v6ij7k$195lj$2@dont-email.me>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu> <v6crrk$1kfb$6@dont-email.me>
<294tlk-pqr1.ln1@q957.zbmc.eu>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 09 Jul 2024 07:52:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="04bb535b0c0a3ec0d50e843b1924189b";
logging-data="1349299"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Z43zefxdK//DPB2mdUk9v"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:w4HEnXpar1RDQWP7FqfgNpWMp5c=
View all headers

On Mon, 8 Jul 2024 13:56:34 +0100, Chris Green wrote:

> 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.

You probably don’t need to define any nontrivial kind of protocol at all.
Just accept a connection from a client, send it the current data in some
convenient format (e.g. JSON), and disconnect.

Subject: Re: Best (simplest) way to share data
From: Chris Green
Newsgroups: comp.lang.python
Date: Tue, 9 Jul 2024 10:02 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: cl@isbd.net (Chris Green)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data
Date: Tue, 9 Jul 2024 11:02:39 +0100
Lines: 22
Message-ID: <veevlk-4u22.ln1@q957.zbmc.eu>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu> <v6crrk$1kfb$6@dont-email.me> <294tlk-pqr1.ln1@q957.zbmc.eu> <server-20240708162329@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net lzGABpU0v9dhgw9w4aKh1QTmoZ2z55ngVXVyxG2HgV2wZZL/I=
X-Orig-Path: not-for-mail
Cancel-Lock: sha1:9c0kpXd0S/dHnK7z8YeCRDwzh+o= sha256:Daw3pdjcgre2gHCaeQ92CpuSvgoLuLHkVjp8IV6P4f4=
User-Agent: tin/2.6.4-20240224 ("Banff") (Linux/6.8.0-36-generic (x86_64))
View all headers

Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> 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:
>
[snip]

Thanks, that should get me started! :-)

--
Chris Green
·

1

rocksolid light 0.9.8
clearnet tor