So I have a terrible little code here
import threading
import time
hours, mins, secs, = 0,0,0
def countUp():
# so I can get data from get_time
global hours, mins, secs
# a game clock thats meant to run in the background
while True:
# if mins and secs are 59, add an hour
if mins == 59 and secs == 59:
hours += 1
# if not, don't add anything
else:
hours += 0
# adds a minute when 60 seconds is up (if its 59, it'll print 60 idk why)
if secs == 59:
mins += 1
# after 60 seconds is up, mins would reset to 0 otherwise
elif mins <= 59:
mins += 0
# when mins == 60, reset to 0
else:
mins = 0
# adds a seconds while a minute isn't up
if secs <= 58:
secs += 1
else:
# resets seconds when a minute is up
secs = 0
time.sleep(1)
def get_time():
# get time from countUp
global hours, mins, secs
# print it
print(f'{hours}:{mins}:{secs} passed')
counter = threading.Thread(target=countUp())
counter.start()
print('This is the base code')
get_time()
It is very scuffed but does the job. If I run it though, the thread works but nothing below counter.start()
runs. I tried changing the while true loop to a for loop and the main program runs after the function has finished.
I put the main program in a function of it's own and threaded it, but nothing changed.
As for multiprocessing, I don't know what I did but countUp
didn't seem to have called. I did import multiprocessing
and change
counter = threading.Thread(target=countUp())
counter.start()
to
if __name__ == '__main__':
p = multiprocessing.Process(target=countUp, args=())
p.start()
p.join()
ps. i know this code is very bad