Last active
May 15, 2019 13:51
-
-
Save emrehorsanali/1bf26e9af9b3c2bace67f4fd0df8ec55 to your computer and use it in GitHub Desktop.
Python Access Data (Flag) From Another Thread (Stop Program with CTRL-Z or Wait Until It Stops)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import threading | |
import time | |
import sys | |
def thread_function(name): | |
global flag1, flag2 | |
time.sleep(10) | |
flag1 = True | |
logging.info("Thread %s: starting FLAG1", name) | |
time.sleep(10) | |
flag2 = True | |
logging.info("Thread %s: starting FLAG2", name) | |
time.sleep(10) | |
def main_thread_function(name): | |
global flag1, flag2 | |
flag1 = False | |
flag2 = False | |
x = threading.Thread(target=thread_function, args=(1,)) | |
x.daemon = True | |
x.start() | |
i = 0 | |
while(i != 25): | |
logging.info(flag1) | |
logging.info(flag2) | |
time.sleep(5) | |
i += 1 | |
if __name__ == "__main__": | |
format = "%(asctime)s: %(message)s" | |
logging.basicConfig(format=format, level=logging.INFO, | |
datefmt="%H:%M:%S") | |
x = threading.Thread(target=main_thread_function, args=(1,)) | |
x.daemon = True | |
x.start() | |
x.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment