Dec-28-2024, 03:01 PM
(This post was last modified: Dec-28-2024, 04:31 PM by Gribouillis.)
Hi,
I have been watching videos and looking up a solution to this for the past day but still cant seem to wrap my mind around this. If there is another thread that explains this please point it out to me.
In simple words I want to have two .py files that have a particular aspect to them like this. So long as A is running, B will stop. And so long as B is running A will stop.
I am not going to go into great detail of what the actual situation is just to save you all the details. I have a mock up situation which closely resembles what I am actually dealing with with my esp32 and a sort of multifunction timer +.
The mock up situation looks like this. A count which continually prints out a count, '1.. 2.. 3.."
Then on the other hand there is a timer. Just a standard timer like would be used while cooking or for studying. However this program I also have printing out 'The clock is on..' just so I can see if what I am attempting to do has been succesfull yet. Which as mention above is that whenever the counter is on, the timer is off, vise versa.
After playing around with it for a while I turned to chatgpt for help so these mock-ups are actually written by it mostly and not me.
I am trying to do this through a shared file which each of the two individual programs will update their status to, "clock_status" and "numberator_status"
For instance:
'clock_status' is set to 1 within the clock program like this
"shared.clock_status == 1". Then it sets the corresponding numberator status to 0
like this,
"shared.numberator_status == 0"
If not clear the shared module is called shared.py
here is the script from chat gpt:
I have been watching videos and looking up a solution to this for the past day but still cant seem to wrap my mind around this. If there is another thread that explains this please point it out to me.
In simple words I want to have two .py files that have a particular aspect to them like this. So long as A is running, B will stop. And so long as B is running A will stop.
I am not going to go into great detail of what the actual situation is just to save you all the details. I have a mock up situation which closely resembles what I am actually dealing with with my esp32 and a sort of multifunction timer +.
The mock up situation looks like this. A count which continually prints out a count, '1.. 2.. 3.."
Then on the other hand there is a timer. Just a standard timer like would be used while cooking or for studying. However this program I also have printing out 'The clock is on..' just so I can see if what I am attempting to do has been succesfull yet. Which as mention above is that whenever the counter is on, the timer is off, vise versa.
After playing around with it for a while I turned to chatgpt for help so these mock-ups are actually written by it mostly and not me.
I am trying to do this through a shared file which each of the two individual programs will update their status to, "clock_status" and "numberator_status"
For instance:
'clock_status' is set to 1 within the clock program like this
"shared.clock_status == 1". Then it sets the corresponding numberator status to 0
like this,
"shared.numberator_status == 0"
If not clear the shared module is called shared.py
here is the script from chat gpt:
# shared.py import threading numberator_status = 1 # 1 means numberator is running, 0 means stopped clock_status = 0 # 0 means clock is off, 1 means clock is running lock = threading.Lock() import time import shared import threading #play_module.py # Function to generate numbers def number_generator(): i = 0 while True: with shared.lock: # Lock to ensure thread safety if shared.numberator_status == 0: # Stop the generator if numberator_status is 0 print("Numberator stopped.") return # Exit the thread gracefully when stopped print(f"Generated number: {i}") i += 1 time.sleep(2) # Simulate number generation with a delay # Function to start the numberator def start_numberator(): with shared.lock: shared.numberator_status = 1 # Set numberator to running print("Numberator started.") # Run the number generator task in its own thread def run_number_generator(): start_numberator() number_generator_thread = threading.Thread(target=number_generator) number_generator_thread.start() # Initially start the numberator if needed run_number_generator() #variable_clock.py import time import shared import threading # Clock function (simulated task) def clock_task(): while True: with shared.lock: # Lock to ensure thread safety if shared.clock_status == 0: # Stop the clock if clock_status is 0 print("Clock stopped.") return # Exit the thread gracefully when stopped print("Clock is running...") time.sleep(1) # Function to start the clock def start_clock(): with shared.lock: shared.clock_status = 1 # Set clock to running shared.numberator_status = 0 # Stop the numberator when clock starts print("Clock started.") # Function to stop the clock def stop_clock(): with shared.lock: shared.clock_status = 0 # Set clock to stopped print("Clock stopped.") # Run the clock task in its own thread def run_clock(): start_clock() clock_thread = threading.Thread(target=clock_task) clock_thread.start() # Start the clock run_clock()I already tried a few different methods of having the two individual files update in to the shared.py file but for some reason it just doesnt seem to work. Is it the case that the only way to do this is to have them all in the same .py file? I dont think so, I just don't have experience to know.
Gribouillis write Dec-28-2024, 04:31 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.