所以我这里有一个糟糕的小代码
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()
它非常磨损,但可以完成工作。如果我运行它,该线程可以运行,但低于 counter.start()
的任何内容都不会运行。我尝试将 while true 循环更改为 for 循环,主程序在函数完成后运行。
我将主程序放在它自己的函数中并对其进行线程化,但没有任何改变。
至于多处理,我不知道我做了什么,但 countUp
似乎没有调用。我做了import multiprocessing
并改变了
counter = threading.Thread(target=countUp())
counter.start()
到
if __name__ == '__main__':
p = multiprocessing.Process(target=countUp, args=())
p.start()
p.join()
ps。我知道这段代码很糟糕