IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> Multithreading | 嵌套循环语句的threading -> 正文阅读

[JavaScript知识库]Multithreading | 嵌套循环语句的threading

Multithreading

文本主要介绍嵌套循环语句下的threading的调控。


嵌套循环语句的threading


示例

每个loop耗时looptime,总耗时runTime,总主循环次数loopCounts

示例1

> 一个主循环loop耗时20s,总耗时120s,总主循环次数10
> 要么满足总耗时要么满足总主循环次数则退出循环,而此处120 / 20 = 6,即在循环6个主循环后会自动退出大循环。
> 每个loop中调用的BOSS函数耗时约4s,所以一个looptime中可循环BOSS10次,即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                                                   # total loop time
    looptime = 20                                                   # single loop time
    loopCounts = 10                                                 # total loop counts

    loop = 0
    start = time.time()
    while (loop < loopCounts) and (time.time()-start < runTime):    # if loop is larger than loop counts or duration
        logging.info(f"****** loop {loop} ******")                  # is larger than 'runTime', break and exit the 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.")

结果如下显示:
一个主循环耗时20s,即looptime
一个子循环耗时约4s,即一个主循环有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耗时2s,总耗时120s,总主循环次数20
> 要么满足总耗时要么满足总主循环次数则退出循环,而此处120 / 2 = 60,即在循环20个主循环后会自动退出大循环。
> 每个loop中调用的BOSS函数耗时约4s,然而looptime2s,所以2 % 4 = 1主循环会以4s为一个循环,20个主循环即总耗时80s。

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                                                   # total loop time
    looptime = 2                                                   # single loop time
    loopCounts = 20                                                 # total loop counts

    loop = 0
    start = time.time()
    while (loop < loopCounts) and (time.time()-start < runTime):    # if loop is larger than loop counts or duration
        logging.info(f"****** loop {loop} ******")                  # is larger than 'runTime', break and exit the 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.")

结果如下显示:
一个主循环耗时2s,即looptime
一个子循环耗时约4s,即一个主循环有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.

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-01-14 01:52:39  更:2022-01-14 01:55:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 12:25:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码