1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Polling in python

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    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()

    Continue reading...

Compartilhe esta Página