Re: Best use of "open" context manager

2024-07-07 Thread Cameron Simpson via Python-list
On 07Jul2024 22:22, Rob Cliffe wrote: it's legal, but doesn't work (trying to access the file after "with f" raises the same     ValueError: I/O operation on closed file. Just to this: of course. The with closes the file. But my version runs the with after the try/except. -- https://mail.py

Re: Best use of "open" context manager

2024-07-07 Thread Cameron Simpson via Python-list
On 07Jul2024 22:22, Rob Cliffe wrote: Remember, the `open()` call returns a file object _which can be used as a context manager_. It is separate from the `with` itself. Did you test this?     f = open(FileName) as f: is not legal syntax. No. You're right, remove the "as f:". it's legal, but

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

2024-07-07 Thread MRAB via Python-list
On 2024-07-07 23:27, Barry via Python-list wrote: On 7 Jul 2024, at 22:13, Chris Green via Python-list 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.

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

2024-07-07 Thread Barry via Python-list
> On 7 Jul 2024, at 22:13, Chris Green via Python-list > 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 th

Re: Best use of "open" context manager

2024-07-07 Thread Rob Cliffe via Python-list
On 07/07/2024 02:08, Cameron Simpson wrote: On 06Jul2024 11:49, Rob Cliffe wrote: try:     f = open(FileName) as f:     FileLines = f.readlines() except FileNotFoundError:     print(f"File {FileName} not found")     sys.exit() # I forgot to put "f.close()" here -:) for ln in File Lines:      

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

2024-07-07 Thread Gordinator via Python-list
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 s

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

2024-07-07 Thread Piergiorgio Sartor via Python-list
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 t

Best (simplest) way to share data between processes

2024-07-07 Thread Chris Green via Python-list
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 (quit