Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196409

Re: Best (simplest) way to share data between processes

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Left Right <olegsivokon@gmail.com>
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> (permalink)
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>
Xref csiph.com comp.lang.python:196409

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Best (simplest) way to share data between processes Chris Green <cl@isbd.net> - 2024-07-06 08:28 +0100
  Re: Best (simplest) way to share data between processes Gordinator <gordinator@gordinator.org> - 2024-07-06 20:03 +0100
  Re: Best (simplest) way to share data between processes Piergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de> - 2024-07-06 21:32 +0200
    Re: Best (simplest) way to share data between processes Chris Green <cl@isbd.net> - 2024-07-08 13:52 +0100
  Re: Best (simplest) way to share data (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-07-07 01:43 +0000
    Re: Best (simplest) way to share data (Posting On Python-List Prohibited) Chris Green <cl@isbd.net> - 2024-07-08 13:56 +0100
      Re: Best (simplest) way to share data (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-07-09 05:52 +0000
      Re: Best (simplest) way to share data Chris Green <cl@isbd.net> - 2024-07-09 11:02 +0100
  Re: Best (simplest) way to share data between processes Barry <barry@barrys-emacs.org> - 2024-07-07 23:27 +0100
  Re: Best (simplest) way to share data between processes MRAB <python@mrabarnett.plus.com> - 2024-07-07 23:47 +0100
  Re: Best (simplest) way to share data between processes Barry Scott <barry@barrys-emacs.org> - 2024-07-08 09:34 +0100
  Re: Best (simplest) way to share data between processes Left Right <olegsivokon@gmail.com> - 2024-07-07 23:55 +0200

csiph-web