I would like to set up a system with python3 that would launch a function each time an event is called. I would like to rewrite this javascript code in python
const event = new Event('myevent');
// Listen for the event.
elem.addEventListener('myevent', function (e) { /* ... */ }, false);
// Dispatch the event.
elem.dispatchEvent(event);
I wrote tried this code but the function runs only once
import threading
import time
event = threading.Event()
x = 0
def myFunction():
global x
event.wait()
if event.is_set():
print("event is set : " + str(x))
x += 1
time.sleep(0.3)
th1 = threading.Thread(target=myFunction)
th1.start()
while True:
event.set()
time.sleep(1)
event.clear()