Skip to content

Instantly share code, notes, and snippets.

@emrehorsanali
Last active May 15, 2019 13:51
Show Gist options
  • Save emrehorsanali/1bf26e9af9b3c2bace67f4fd0df8ec55 to your computer and use it in GitHub Desktop.
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)
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