import threading
import time
# Create an Event object
stop_event = threading.Event()
def dump(iterations):
for i in range(iterations):
print('Dump Iteration ',)
def thread_function(iterations, stop_event):
if stop_event.is_set():
print("Thread stopping...")
else:
dump(iterations)
def main():
t = threading.Thread(target=thread_function, args=(20, stop_event))
t.start()
# Main thread work
for i in range(10):
print(f"Main thread iteration {i}")
time.sleep(1) # Simulate work
# Signal the thread to stop
stop_event.set()
t.join() # Wait for the thread to finish
if __name__ == "__main__":
main()
aW1wb3J0IHRocmVhZGluZwppbXBvcnQgdGltZQoKIyBDcmVhdGUgYW4gRXZlbnQgb2JqZWN0CnN0b3BfZXZlbnQgPSB0aHJlYWRpbmcuRXZlbnQoKQpkZWYgZHVtcChpdGVyYXRpb25zKToKICAgIGZvciBpIGluIHJhbmdlKGl0ZXJhdGlvbnMpOgogICAgICAgIHByaW50KCdEdW1wIEl0ZXJhdGlvbiAnLCkKZGVmIHRocmVhZF9mdW5jdGlvbihpdGVyYXRpb25zLCBzdG9wX2V2ZW50KToKICAgIGlmIHN0b3BfZXZlbnQuaXNfc2V0KCk6CiAgICAgICAgcHJpbnQoIlRocmVhZCBzdG9wcGluZy4uLiIpCiAgICBlbHNlOgogICAgICAgIGR1bXAoaXRlcmF0aW9ucykKCmRlZiBtYWluKCk6CiAgICB0ID0gdGhyZWFkaW5nLlRocmVhZCh0YXJnZXQ9dGhyZWFkX2Z1bmN0aW9uLCBhcmdzPSgyMCwgc3RvcF9ldmVudCkpCiAgICB0LnN0YXJ0KCkKCiAgICAjIE1haW4gdGhyZWFkIHdvcmsKICAgIGZvciBpIGluIHJhbmdlKDEwKToKICAgICAgICBwcmludChmIk1haW4gdGhyZWFkIGl0ZXJhdGlvbiB7aX0iKQogICAgICAgIHRpbWUuc2xlZXAoMSkgICMgU2ltdWxhdGUgd29yawoKICAgICMgU2lnbmFsIHRoZSB0aHJlYWQgdG8gc3RvcAogICAgc3RvcF9ldmVudC5zZXQoKQogICAgdC5qb2luKCkgICMgV2FpdCBmb3IgdGhlIHRocmVhZCB0byBmaW5pc2gKCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6CiAgICBtYWluKCkK