0

Here is my desired functionality:

I want to listen over the local host for a variable, let's say state 1 and state 2. I want my main script to execute function b, but if it receives a state 1 over the local host, I want it to interrupt whatever it's doing (almost like a control-c), and start executing function A.

My issue is with the polling, I can't seem to get the two to work independently of each other, it's waiting for the execution of function b to finish before checking to see if the state over local host has changed.

Essentially I want the function to check weather the state has changed and whether it needs to switch what it executes in parallel.

Can anyone guide me in the right direction or point out my mistake?

Side note: I am simulating the change in state for now for simplicity's sake.


import random
import threading
import time

state_lock = threading.Lock()
current_state = 1  # 1 for normal state, 2 for fall detected state
fall_detected = False


# Function for the normal task
def normal_task():
    global current_state
    task_duration = 20  
    elapsed = 0
    while elapsed < task_duration:
        with state_lock:
            if current_state == 1:
                print("Normal Task")
                time.sleep(1) 
                elapsed += 1  
            else:
                print("Fall detected")
                break  
        
def fall_task():
    global current_state
    while True:
        with state_lock:
            if current_state == 2:
                print("Executing Fall Detected")
                time.sleep(2)  
                current_state = 1
                print("Fall handled")
                break  
        time.sleep(0.5)

# Function to simulate fall detection signal and switch states
def fall_detection_signal():
    global current_state, fall_detected
    previous_time = time.time()  
    while True:
        current_time = time.time()  
        elapsed_time = (
            current_time - previous_time
        )  
        previous_time = current_time  
        print(f"Time since last fall detection check: {elapsed_time:.2f} seconds")
        time.sleep(
            random.uniform(5, 10)
        )  # Simulate random time for fall detection check
        with state_lock:
            fall_detected = True
            current_state = 2
            print("Fall Detected! Switching state to Fall Detected.")
        time.sleep(5)  
        fall_detected = False


normal_task_thread = threading.Thread(target=normal_task)
fall_detection_thread = threading.Thread(target=fall_detection_signal)

normal_task_thread.start()
fall_detection_thread.start()

while True:
    if fall_detected:
        fall_task_thread = threading.Thread(target=fall_task)
        fall_task_thread.start()
        fall_task_thread.join()

    if current_state == 1:
        normal_task_thread = threading.Thread(target=normal_task)
        normal_task_thread.start()
        normal_task_thread.join()
2
  • 1
    Please review your question: Your code works, I see in stdout Normal Task -> Fall Detected! Switching state to Fall Detected. -> Fall detected -> Executing Fall Detected -> Fall handled -> Normal Task that looks like what you described ?! Which behaviour do you expect ? Btw. a thread cannot stop another thread. Commented Sep 11, 2024 at 21:16
  • I don't think it is doing it asyn, forexample it is first waiting for normal task to finish before it polls to see if the status has changed. Oh and I see I did not know that, is there a way to have a thread stop another thread or something similar in functionality. Commented Sep 12, 2024 at 19:54

1 Answer 1

0

After experimenting a bit more and considering python 2.7 I would say the undertaking of interrupting another thread's execution when detecting a flag is impossible. The best you can do through using a single script is have it poll a variable and simply switch the execution once the previous task/state is done. you would almost need one thread to control-c (interrupt) another thread for it to instantaneously switch which thread is executing based on a detected variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Consider adding code samples based on the code provided in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.