python3 线程 锁 线程池

线程使用

 

方式1 创建并立即执行

import _thread as thread
import time


# 为线程定义一个函数
def customize_function(thread_name, delay):
    sum = 0
    while sum < 5:
        time.sleep(delay)  # 休眠时间,单位秒
        sum += 1
        print("%s: %s" % (thread_name, time.ctime(time.time())))


# 创建两个线程
try:
    thread.start_new_thread(customize_function, ("Thread-1", 3,))
    thread.start_new_thread(customize_function, ("Thread-2", 6,))
except:
    print("Error: 无法启动线程")
finally:
    print("主线程执行")
while 1:  # 主线程结束之后,子线程也会结束,主线程不能结束
    pass

 

2.自定义线程对象并阻塞

import threading
import time

class customize_thread(threading.Thread):

    # 自定义线程对象初始化
    def __init__(self, thread_id, name):
        threading.Thread.__init__(self)
        self.thread_id = thread_id
        self.name = name

    # 线程运行调用方法
    def run(self):
        print("执行线程:" + self.name)
        print_time(self)
        print("退出线程:" + self.name)


# 模拟单个线程执行的内容
def print_time(self):
    for i in range(5):
        time.sleep(1)
        print("%s: %s" % (self.name, time.ctime(time.time())))


# 创建新线程
thread1 = customize_thread(1, "线程1")
thread2 = customize_thread(2, "线程2")

# 开启新线程
thread1.start()
thread2.start()
# 阻塞主线程,等待线程1完成,最多等待1秒,1秒过后继续执行主线程,线程1不会停止
thread1.join(1)
# 阻塞主线程,等待线程2完成
thread2.join()
print("退出主线程")

 

3.使用锁让线程同步

#!/usr/bin/python3

import threading
import time


class customize_thread(threading.Thread):
    # 自定义线程对象初始化
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    # 线程运行调用方法
    def run(self):
        print("执行线程: " + self.name)
        threadLock.acquire()  # 获取锁
        print_time(self)
        threadLock.release()  # 释放锁


# 模拟单个线程执行的内容
def print_time(self):
    for i in range(5):
        time.sleep(1)
        print("%s: %s" % (self.name, time.ctime(time.time())))


# 添加锁对象
threadLock = threading.Lock()
# 创建线程
thread1 = customize_thread(1, "线程1")
thread2 = customize_thread(2, "线程2")
# 启动线程
thread1.start()
thread2.start()
# 主线程等待所有线程完成
thread1.join()
thread2.join()
print("退出主线程")

 

4.线程安全队列

import queue

work_queue = queue.Queue(10)  # 创建指定长度为10的队列
[work_queue.put(i) for i in range(9)]  # 入队列
print("队列大小", work_queue.qsize())
for i in range(9): print(work_queue.get())  # 出队列
print("队列大小", work_queue.qsize())

 

5.线程池

from concurrent.futures import ThreadPoolExecutor
import time


# 单个线程执行的内容
def func(a):
    print("start: " + a)
    time.sleep(2)
    print("end: " + a)


unit_arr = ["1", "2", "3", "4", "5", "6", "7"]  # 定义需要执行的任务
executor = ThreadPoolExecutor(3)
[executor.submit(func, unit) for unit in unit_arr]  # 单个任务添加与批量添加 executor.map(func, unit_arr)
print("执行完成")

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钻风巡山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值