read() and read_write() should run alternatively (the two are never in parallel)
detect() function establish which one of the two above thread functions is running and pauses the other thread
I've used Event to do this
Code: Select all
import threading
import numpy as np
from time import sleep
def read():
event_rd.wait()
print("read is running")
def read_write():
event_rdw.wait()
print("read_write is running")
def detect():
while True:
x= np.random.randint(1,100)
if x > 50:
event_rd.set()
if x < 50:
event_rdw.set()
sleep(2)
event_rd = threading.Event # restart read() and pause read_write()
event_rdw = threading.Event # restart read_write ans pause read()
t_main = threading.Thread(target=detect, name="detect")
t_main.start()
t_rd = threading.Thread(target=read, name="read")
t_rdw = threading.Thread(target=read_write, name="read_write")
t_rd.start()
t_rdw.start()