Multithreading
文本主要介绍嵌套循环语句下的threading的调控。
示例
每个loop耗时looptime ,总耗时runTime ,总主循环次数loopCounts 。
示例1
> 一个主循环loop耗时20 s,总耗时120 s,总主循环次数10 。 > 要么满足总耗时要么满足总主循环次数则退出循环,而此处120 / 20 = 6 ,即在循环6个主循环后会自动退出大循环。 > 每个loop中调用的BOSS 函数耗时约4 s,所以一个looptime 中可循环BOSS 10次,即10个Subloop 。
import datetime, time
import threading
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s: %(levelname)s %(message)s',
datefmt='%Y-%m-%d %A %H:%M:%S',
filename='logging.log',
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(filename)s : %(levelname)s %(message)s')
console.setFormatter(formatter)
logging.getLogger().addHandler(console)
log = logging.getLogger(__name__)
def worker(name, sleep=2):
logging.info(f"Worker '{name}': {sleep} s.")
time.sleep(sleep)
logging.info(f"Worker '{name}': done.")
def boss(name, sleep=4):
logging.info(f"Boss '{name}': {sleep} s.")
time.sleep(sleep)
logging.info(f"Boss '{name}': done.")
def ThreadAliveOrNot(thread):
if not thread.is_alive():
logging.info(f"{thread} Not Alive.")
else:
logging.info(f"{thread} Alive")
if __name__ == '__main__':
runTime = 120
looptime = 20
loopCounts = 10
loop = 0
start = time.time()
while (loop < loopCounts) and (time.time()-start < runTime):
logging.info(f"****** loop {loop} ******")
START = time.time()
i = 0
while time.time()-START < looptime:
logging.info(f"****** Sub loop {i} ******")
Subthread = threading.Thread(target=worker, kwargs={'name': 'Hanna', 'sleep': 4}, name='WORKER')
ThreadAliveOrNot(thread=Subthread)
Subthread.start()
ThreadAliveOrNot(thread=Subthread)
boss(name='BOSS', sleep=2)
Subthread.join()
ThreadAliveOrNot(thread=Subthread)
i += 1
logging.info("Post Loop")
loop += 1
else:
logging.info("> End.")
结果如下显示: 一个主循环耗时20 s,即looptime 。 一个子循环耗时约4 s,即一个主循环有5个Subloop 。
2022-01-11 00:03:34,635 Multithreading_While.py : INFO ****** loop 0 ******
2022-01-11 00:03:34,635 Multithreading_While.py : INFO ****** Sub loop 0 ******
2022-01-11 00:03:34,635 Multithreading_While.py : INFO <Thread(WORKER, initial)> Not Alive.
2022-01-11 00:03:34,635 Multithreading_While.py : INFO Worker 'Hanna': 4 s.
2022-01-11 00:03:34,635 Multithreading_While.py : INFO <Thread(WORKER, started 6115717120)> Alive
2022-01-11 00:03:34,635 Multithreading_While.py : INFO Boss 'BOSS': 2 s.
2022-01-11 00:03:36,640 Multithreading_While.py : INFO Boss 'BOSS': done.
2022-01-11 00:03:38,640 Multithreading_While.py : INFO Worker 'Hanna': done.
2022-01-11 00:03:38,641 Multithreading_While.py : INFO <Thread(WORKER, stopped 6115717120)> Not Alive.
2022-01-11 00:03:38,641 Multithreading_While.py : INFO ****** Sub loop 1 ******
2022-01-11 00:03:38,641 Multithreading_While.py : INFO <Thread(WORKER, initial)> Not Alive.
2022-01-11 00:03:38,641 Multithreading_While.py : INFO Worker 'Hanna': 4 s.
2022-01-11 00:03:38,641 Multithreading_While.py : INFO <Thread(WORKER, started 6115717120)> Alive
2022-01-11 00:03:38,641 Multithreading_While.py : INFO Boss 'BOSS': 2 s.
2022-01-11 00:03:40,646 Multithreading_While.py : INFO Boss 'BOSS': done.
2022-01-11 00:03:42,646 Multithreading_While.py : INFO Worker 'Hanna': done.
2022-01-11 00:03:42,647 Multithreading_While.py : INFO <Thread(WORKER, stopped 6115717120)> Not Alive.
2022-01-11 00:03:42,647 Multithreading_While.py : INFO ****** Sub loop 2 ******
2022-01-11 00:03:42,647 Multithreading_While.py : INFO <Thread(WORKER, initial)> Not Alive.
2022-01-11 00:03:42,648 Multithreading_While.py : INFO Worker 'Hanna': 4 s.
2022-01-11 00:03:42,648 Multithreading_While.py : INFO <Thread(WORKER, started 6115717120)> Alive
2022-01-11 00:03:42,648 Multithreading_While.py : INFO Boss 'BOSS': 2 s.
2022-01-11 00:03:44,651 Multithreading_While.py : INFO Boss 'BOSS': done.
2022-01-11 00:03:46,653 Multithreading_While.py : INFO Worker 'Hanna': done.
2022-01-11 00:03:46,653 Multithreading_While.py : INFO <Thread(WORKER, stopped 6115717120)> Not Alive.
2022-01-11 00:03:46,653 Multithreading_While.py : INFO ****** Sub loop 3 ******
2022-01-11 00:03:46,653 Multithreading_While.py : INFO <Thread(WORKER, initial)> Not Alive.
2022-01-11 00:03:46,653 Multithreading_While.py : INFO Worker 'Hanna': 4 s.
2022-01-11 00:03:46,654 Multithreading_While.py : INFO <Thread(WORKER, started 6115717120)> Alive
2022-01-11 00:03:46,654 Multithreading_While.py : INFO Boss 'BOSS': 2 s.
2022-01-11 00:03:48,659 Multithreading_While.py : INFO Boss 'BOSS': done.
2022-01-11 00:03:50,659 Multithreading_While.py : INFO Worker 'Hanna': done.
2022-01-11 00:03:50,659 Multithreading_While.py : INFO <Thread(WORKER, stopped 6115717120)> Not Alive.
2022-01-11 00:03:50,659 Multithreading_While.py : INFO ****** Sub loop 4 ******
2022-01-11 00:03:50,659 Multithreading_While.py : INFO <Thread(WORKER, initial)> Not Alive.
2022-01-11 00:03:50,659 Multithreading_While.py : INFO Worker 'Hanna': 4 s.
2022-01-11 00:03:50,659 Multithreading_While.py : INFO <Thread(WORKER, started 6115717120)> Alive
2022-01-11 00:03:50,659 Multithreading_While.py : INFO Boss 'BOSS': 2 s.
2022-01-11 00:03:52,664 Multithreading_While.py : INFO Boss 'BOSS': done.
2022-01-11 00:03:54,664 Multithreading_While.py : INFO Worker 'Hanna': done.
2022-01-11 00:03:54,665 Multithreading_While.py : INFO <Thread(WORKER, stopped 6115717120)> Not Alive.
2022-01-11 00:03:54,665 Multithreading_While.py : INFO Post Loop
2022-01-11 00:03:54,665 Multithreading_While.py : INFO ****** loop 1 ******
2022-01-11 00:03:54,665 Multithreading_While.py : INFO ****** Sub loop 0 ******
2022-01-11 00:03:54,665 Multithreading_While.py : INFO <Thread(WORKER, initial)> Not Alive.
......
......
......
2022-01-11 00:05:14,774 Multithreading_While.py : INFO ****** loop 5 ******
......
2022-01-11 00:05:34,797 Multithreading_While.py : INFO Post Loop
2022-01-11 00:05:34,797 Multithreading_While.py : INFO > End.
示例2
> 一个主循环loop耗时2 s,总耗时120 s,总主循环次数20 。 > 要么满足总耗时要么满足总主循环次数则退出循环,而此处120 / 2 = 60 ,即在循环20 个主循环后会自动退出大循环。 > 每个loop中调用的BOSS 函数耗时约4 s,然而looptime 为2 s,所以2 % 4 = 1 主循环会以4 s为一个循环,20 个主循环即总耗时80 s。
def worker(name, sleep=2):
logging.info(f"Worker '{name}': {sleep} s.")
time.sleep(sleep)
logging.info(f"Worker '{name}': done.")
def boss(name, sleep=4):
logging.info(f"Boss '{name}': {sleep} s.")
time.sleep(sleep)
logging.info(f"Boss '{name}': done.")
def ThreadAliveOrNot(thread):
if not thread.is_alive():
logging.info(f"{thread} Not Alive.")
else:
logging.info(f"{thread} Alive")
if __name__ == '__main__':
runTime = 120
looptime = 2
loopCounts = 20
loop = 0
start = time.time()
while (loop < loopCounts) and (time.time()-start < runTime):
logging.info(f"****** loop {loop} ******")
START = time.time()
i = 0
while time.time()-START < looptime:
logging.info(f"****** Sub loop {i} ******")
Subthread = threading.Thread(target=worker, kwargs={'name': 'Hanna', 'sleep': 2}, name='WORKER')
ThreadAliveOrNot(thread=Subthread)
Subthread.start()
ThreadAliveOrNot(thread=Subthread)
boss(name='BOSS', sleep=4)
Subthread.join()
ThreadAliveOrNot(thread=Subthread)
i += 1
else:
logging.info("Post Loop")
loop += 1
else:
logging.info("> End.")
结果如下显示: 一个主循环耗时2 s,即looptime 。 一个子循环耗时约4 s,即一个主循环有1个Subloop 。
2022-01-11 00:24:52,364 Multithreading_While.py : INFO ****** loop 0 ******
2022-01-11 00:24:52,364 Multithreading_While.py : INFO ****** Sub loop 0 ******
2022-01-11 00:24:52,364 Multithreading_While.py : INFO <Thread(WORKER, initial)> Not Alive.
2022-01-11 00:24:52,364 Multithreading_While.py : INFO Worker 'Hanna': 2 s.
2022-01-11 00:24:52,364 Multithreading_While.py : INFO <Thread(WORKER, started 6111932416)> Alive
2022-01-11 00:24:52,364 Multithreading_While.py : INFO Boss 'BOSS': 4 s.
2022-01-11 00:24:54,365 Multithreading_While.py : INFO Worker 'Hanna': done.
2022-01-11 00:24:56,365 Multithreading_While.py : INFO Boss 'BOSS': done.
2022-01-11 00:24:56,365 Multithreading_While.py : INFO <Thread(WORKER, stopped 6111932416)> Not Alive.
2022-01-11 00:24:56,365 Multithreading_While.py : INFO Post Loop
2022-01-11 00:24:56,365 Multithreading_While.py : INFO ****** loop 1 ******
2022-01-11 00:24:56,365 Multithreading_While.py : INFO ****** Sub loop 0 ******
......
......
......
2022-01-11 00:26:08,466 Multithreading_While.py : INFO ****** loop 19 ******
......
2022-01-11 00:26:12,472 Multithreading_While.py : INFO <Thread(WORKER, stopped 6111932416)> Not Alive.
2022-01-11 00:26:12,472 Multithreading_While.py : INFO Post Loop
2022-01-11 00:26:12,473 Multithreading_While.py : INFO > End.
|