python enumerate for 使用 主线程和子线程的创建和执行学习笔记

 

import cv2 as cv
str = "Hope is a good thing and maybe the best of things. And no good thing ever dies."
names = ["Hope", "is", "a", "good"]


def main():
    for i, name in enumerate(names):
        print(i, name)

    for tempName in enumerate(names):
        print(tempName)

    print(names)
    a, b, c, d = names
    print(a)
    print(b)
    print(c)
    print(d)


if __name__ == '__main__':
    main()

for 多个元素直接写了,** in ***;拆包

 

线程执行没有先后顺序,可以通过延时来控制执行的先后顺序

import threading

def test1():
    for i in range(5):
        print("----test1---------")


def test2():
    for i in range(5):
        print("----Hope is a good thing and maybe the best "
              "of things. And no good thing ever dies.---------")


def main():
    t1 = threading.Thread(target = test1)
    t2 = threading.Thread(target = test2)

    t1.start()
    t2.start()

    print(threading.enumerate())
    

if __name__ == '__main__':
    main()

主线程会等待所有的子线程结束后才结束

每次结果都不一样

查看线程数量

import threading
import time

def test1():
    for i in range(5):
        print("---%d-test1---------" % i)


def test2():
    for i in range(5):
        print("----Hope is a good thing and maybe the best "
              "of things. And no good thing ever dies.---------")


def main():
    t1 = threading.Thread(target = test1)
    t2 = threading.Thread(target = test2)

    t1.start()

    time.sleep(1)
    print("不是春来偏爱酒,应须得酒遣春愁")

    t2.start()

    time.sleep(1)
    print("乌家若下蚁还浮,白玉尊前倒即休。")

    print(threading.enumerate())


if __name__ == '__main__':
    main()

1秒对cpu很长时间,这次就固定了

 

import threading
import time

def test1():
    for i in range(5):
        print("---%d-test1---------" % i)
        time.sleep(1)


def test2():
    for i in range(10):
        print("----Hope is a good thing and maybe the best "
              "of things. And no good thing ever dies.---------")
        time.sleep(1)


def main():
    t1 = threading.Thread(target = test1)
    t2 = threading.Thread(target = test2)

    t1.start()

    # time.sleep(1)
    # print("不是春来偏爱酒,应须得酒遣春愁")

    t2.start()

    # time.sleep(1)
    # print("乌家若下蚁还浮,白玉尊前倒即休。")
    while True:
        print(threading.enumerate())
        if len(threading.enumerate()) <= 1:
            break
        time.sleep(1)


if __name__ == '__main__':
    main()

import threading
import time


def test1():
    for i in range(5):
        print("-----test1---%d---" % i)
        time.sleep(1)


def main():
    # 在调用Thread之前先打印当前线程信息
    print(threading.enumerate())
    t1 = threading.Thread(target=test1)  # 仅仅创建了对象还没有线程

    # 在调用Thread之后打印
    print(threading.enumerate())

    t1.start()

    # 在调用start之后打印
    print(threading.enumerate())

if __name__ == "__main__":
    main()

import threading
import time


def test1():
    for i in range(5):
        print("-----test1---%d---" % i)
        time.sleep(1)

    # 如果创建Thread时执行的函数,运行结束那么意味着 这个子线程结束了....


def test2():
    for i in range(10):
        print("-----test2---%d---" % i)
        time.sleep(1)


def main():
    t1 = threading.Thread(target=test1)
    t2 = threading.Thread(target=test2)

    t1.start()
    t2.start()

    while True:
        print(threading.enumerate())
        if len(threading.enumerate())<=1:
            break
        time.sleep(1)


if __name__ == "__main__":
    main()

 

import threading
import time

def test1():
    for i in range(5):
        print("---%d-test1---------" % i)
        time.sleep(1)


def test2():
    for i in range(10):
        print("----Hope is a good thing and maybe the best "
              "of things. And no good thing ever dies.---------")
        time.sleep(1)


def main():
    t1 = threading.Thread(target = test1)
    t2 = threading.Thread(target = test2)

    t1.start()

    # time.sleep(1)
    # print("不是春来偏爱酒,应须得酒遣春愁")

    t2.start()

    # time.sleep(1)
    # print("乌家若下蚁还浮,白玉尊前倒即休。")
    while True:
        print(threading.enumerate())
        if len(threading.enumerate()) <= 1:
            break
        time.sleep(1)


if __name__ == '__main__':
    main()

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值