Python 多线程开发

简述:python多线程调用方式、线程锁、线程优先级讲解

一、调用方式

库:

import _thread:函数式调用方式,,语法如下_thread.start_new_thread ( function, args[, kwargs] )

import threading:thread1.start() 启动进程

 

_thread:函数式调用方式

import _thread
import time

def print_time(threadname,delay):
    count = 0
    while count < 5:
        print(threading.currentThread())
        time.sleep(delay)
        count += 1
        print("%s:%s"%(threadname, time.ctime(time.time())))

try:
    _thread.start_new_thread(print_time, ("Thread-1",2))
    _thread.start_new_thread(print_time, ("Thread-2",5))
except:
    print("error")

while 1:
    pass

threading:对象启动方式

import threading
import time

exitFlag = 0
class MyThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
        print("线程开始"+self.name)
        print_time(self.name, self.counter, 5)
        print("线程退出"+self.name)

def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            threadName.exit()
        time.sleep(delay)
        print("%s:%s"%(threadName, time.ctime(time.time())))
        counter -= 1

# 创建新线程
thread1 = MyThread(1, 'Thread-1', 1)
thread2 = MyThread(2, 'Thread-2', 2)

# 启动线程
thread1.start()
thread2.start()

thread1.join()
thread2.join()

# 给线程更改名字
print(thread1.getName())
thread1.setName("sfafadfassdf")
print(thread1.getName())

二、线程锁

threadlock = threading.Lock()

threadlock.acquire() # 开启线程锁

threadlock.release() # 释放线程锁

import threading
import time

class Mythread(threading.Thread):
    def __init__(self, threadname, sleeptime):
        threading.Thread.__init__(self)
        self.sleeptime = sleeptime
        self.threadname = threadname

    def run(self):
        print("线程开始")
        threadlock.acquire()  # 开启线程锁
        print_time(self.threadname, self.sleeptime, 5)
        threadlock.release()  # 释放线程锁
        print("%s完成"%self.threadname)

def print_time(threadname, sleeptime, counter):
    while counter > 0:
        time.sleep(sleeptime)
        print("%s:%s"%(threadname,time.ctime(time.time())))
        counter -= 1

threadlock = threading.Lock()
threadlist = []


thread1 = Mythread("thread-1", 2)
thread2 = Mythread("thread-2", 5)

thread1.start()
thread2.start()

threadlist.append(thread1)
threadlist.append(thread2)

for thread in threadlist:
    thread.join()

print('线程都处理完毕')

三、线程优先级队列

Queue:先进先出

LifoQueue:先进后出

PriorityQueue:按优先级执行,纯数字则越小优先级越高

# coding:utf-8
from queue import Queue, LifoQueue, PriorityQueue

# 先进先出
q = Queue(maxsize=5)
# 先进后出
lq = LifoQueue(maxsize=5)
# 按优先级执行,纯数字则越小优先级越高
pq = PriorityQueue(maxsize=5)

for i in range(5):
    q.put(i)
    lq.put(i)
    pq.put(i)

while not pq.empty():
    print("队列长度%d,取出列表%d,%d,%d"%(q.qsize(), q.get(), lq.get(), pq.get()))

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值