Page 1 of 1

Need to stop multiple programs accessing a tiny screen at once

Posted: Fri Nov 30, 2018 2:09 pm
by Andyroo
I'm stuck on how to stop two programs from running at once in Python.

I have one program (call it 'status') that starts at boot via cron that sits just waiting for a switch to be closed - at this point it goes off to check some bits and displays the result on the InkyPhat from Pimoroni a few seconds later for a couple of minutes.

The second program (call it 'web') runs once an hour to pull data off a web site and display that on the same screen.

Both of these bits are not giving me any coding problems that I cannot work through at this time BUT due to the size of the screen I cannot get all the data on it at once (well it is a bit small) so I need the web program not to output while status is running and status not to output while the web program is running. I am limited in physical room for the display else I would buy a bigger screen :cry:

My first thought was to create an execute queue using MQTT and a dispatcher program that monitors this and acts on commands from cron jobs. A bit 'daft' but I was reading up on MQTT and want to practise with it :o

I've thought about creating a file and locking that or putting data in it (PID possibly) and using that but that seems a bit 'old fashioned'. I came across the zc.lockfile module that would simplify this though.

I've read the 'manual' on mutex lock support in the python guide and reasonably sure I can program that. No idea how to check they exist though if the program errors...

I've looked at using semaphores but they only seem to work within one program across multiple threads. Though I could rewrite the programs now in a 'threaded mode', its going to be a pain to debug long term as more and more switch driven status style programs are created.

I've thought about checking for a given program name currently executing - again this will be a mess as more and more programs are created.

I had a quick read up on the built in GIL but that (though interesting) does not seem to apply here and the multiprocess module got me well lost on options to launch programs :lol:

To be honest under the KISS principal the lock file module wins hands out (esp as I can tidy up from the console if needed) but I used that technique many many years ago and would like to try something more modern :lol: :oops: :lol: so, as I have no need for any other type of synchronisation or to pass data between programs, do you have any thoughts on locking processes in Python 3 to help?

TIA

Re: Need to stop multiple programs accessing a tiny screen at once

Posted: Fri Nov 30, 2018 7:42 pm
by ghp
Hello,
you could use a named pipe (standard on linux) for this task: let the data sources write messages into the pipe, and write a third program which reads pipe and displays messages in display for some time.
example: https://www.roman10.net/2011/04/21/name ... n-example/

Re: Need to stop multiple programs accessing a tiny screen at once

Posted: Sat Dec 01, 2018 12:20 am
by Andyroo
Thank you for that link - looks similar to my MQTT idea but without a server in the middle :lol:

I’ve been looking at sample screen layouts and as the data is not very large and can be padded to a fixed length I think I’ll try this and learn something new :)