老男孩python学习自修第二十三天【多线程】

时间:2023-03-09 15:13:46
老男孩python学习自修第二十三天【多线程】

1. 线程的创建与运行

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from threading import Thread

def foo(param1, param2):
    print "{0}{1}".format(param1, param2)

if __name__ == "__main__":

    print "main thread running"

    thread = Thread(target=foo, args=(123, "abc"))

    print "before new thread running"
    thread.start()
    print "after new thread running"

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day23/thread_test.py
main thread running
before new thread running
123abcafter new thread running

Process finished with exit code 0

2.线程常用API

thread.getName()  获取线程的名称,子线程的名称默认为Thread-n

thread.setName(name)  设置线程的名称

thread.isDaemon()  是否为守护线程,是守护线程则主线程结束,子线程结束;子线程不是守护线程则主线程等待子线程结束后才结束

thread.setDaemon(bool)  设置是否为守护线程

thread.start()

thread.run()

thread.join(timeout)

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from threading import Thread
import time

def bar():
    for item in range(100):
        print item
        time.sleep(1)

if __name__ == "__main__":

    print "main thread running"

    thread = Thread(target=bar, args=())
    thread.setDaemon(True)
    print "before %s running" % thread.getName()
    thread.start()
    print "after %s running" % thread.getName()

    time.sleep(5)

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day23/thread_test.py
main thread running
before Thread-1 running
after Thread-1 running
0
1
2
3
4

Process finished with exit code 0

3.join()的使用

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from threading import Thread
import time

def bar():
    for item in range(100):
        print item
        time.sleep(1)

if __name__ == "__main__":

    print "main thread running"

    thread = Thread(target=bar, args=())
    thread.setDaemon(True)
    print "before %s running" % thread.getName()
    thread.start()
    thread.join(10)
    print "after %s running" % thread.getName()

    time.sleep(5)

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day23/thread_test.py
main thread running
before Thread-1 running
0
1
2
3
4
5
6
7
8
9
after Thread-1 running
10
11
12
13
14

Process finished with exit code 0

注意:

(1)thread.join(timeout) 表示主线程等待子线程运行timeout后,主线程再运行

4.自定义线程类

day23

  __init__.py

  mythread.py

  mythread_test.py

mythread.py

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from threading import Thread

class MyThread(Thread):

    def run(self, ):
        print self.getName() + " is running"
        Thread.run(self)

mythread_test.py

#/usr/bin/env python
# _*_ coding:UTF-8 _*_

from day23 import mythread

def foo(param):
    print "Thread running with %s" % param

if __name__ == "__main__":

    thread = mythread.MyThread(target=foo, args=("abc", ))
    thread.start()

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day23/mythread_test.py
Thread-1 is running
Thread running with abc

Process finished with exit code 0

注意:

(1)自定义线程类需要继承threading.Thread类并实现run(),在run()中调用父类的run()

(2)使用自定义的类需要导入,导入和使用为:

from package import module

mythread = module.MyThread()

5.生产者-消费者模型

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

from threading import Thread
from Queue import Queue
import time

class Producer(Thread):

    def __init__(self, name, queue):
        self.__name = name
        self.__queue = queue
        super(Producer, self).__init__()

    def run(self):
        while True:
            if self.__queue.qsize() <= 10:
                self.__queue.put("包子")
                print "%s 生产了一个包子" % (self.__name)
            else:
                time.sleep(1)
        super(Producer,self).run()

class Consumer(Thread):

    def __init__(self, name, queue):
        self.__name = name
        self.__queue = queue
        super(Consumer,self).__init__()

    def run(self):
        while True:
            if not self.__queue.empty():
                self.__queue.get()
                print "%s 消费了一个包子" % (self.__name)
        super(Consumer,self).run()

if __name__ == "__main__":

    queue = Queue(maxsize=200)

    for item in range(3):
        name = "producer %d" % item
        producer = Producer(name, queue)
        producer.start()

    for item in range(100):
        name = "consumer %d" % item
        consumer = Consumer(name, queue)
        consumer.start()

结果:

producer 1 生产了一个包子
consumer 42 消费了一个包子producer 1 生产了一个包子
consumer 42 消费了一个包子

producer 1 生产了一个包子consumer 42 消费了一个包子
producer 1 生产了一个包子 

consumer 99 消费了一个包子producer 1 生产了一个包子
producer 1 生产了一个包子

consumer 94 消费了一个包子
 producer 1 生产了一个包子

6.线程安全锁

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

import threading
import time

num = 0

def run():
    lock.acquire()
    global num
    num += 1
    lock.release()
    time.sleep(0.01)
    print "%s runing: num is %d" % (thread.getName(), num)

if __name__ == "__main__":

    lock = threading.Lock()

    for item in range(2000):
        thread = threading.Thread(target=run, args=())
        thread.setDaemon(False)
        thread.start()

部分结果:

 Thread-1670 runing: num is 1669
Thread-1671 runing: num is 1669
Thread-1674 runing: num is 1674Thread-1674 runing: num is 1674
 Thread-1674 runing: num is 1674

Thread-1681 runing: num is 1680
Thread-1681 runing: num is 1681
Thread-1685 runing: num is 1684
Thread-1685 runing: num is 1684
Thread-1686 runing: num is 1685
Thread-1687 runing: num is 1685
Thread-1697 runing: num is 1696Thread-1697 runing: num is 1696

Thread-1697 runing: num is 1697Thread-1698 runing: num is 1697

注意:

(1)一个线程没有没有进行sleep,则执行100条指令

(2)线程之间是共享内存资源的,为了线程的安全,需要增加线程锁

(3)双重加锁需要使用lock = threading.RLock()

其他锁:

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

import threading
import time

num = 0

def run():
    lock.acquire()
    global num
    num += 1
    lock.release()
    time.sleep(0.01)
    print "%s runing: num is %d" % (thread.getName(), num)

if __name__ == "__main__":
    #这时普通锁
    lock = threading.Lock()
    #这是双重锁
    lock = threading.RLock()
    #这时多个线程共享锁
    lock = threading.BoundedSemaphore(4)

    for item in range(2000):
        thread = threading.Thread(target=run, args=())
        thread.setDaemon(False)
        thread.start()

7.生产者-消费者模型的通信事件

#!/usr/bin/env python
# _*_ coding:UTF-8 _*_

import threading
import time

def producer():
    print "生产者:正在等待消费者到来..."
    # 生产者在这里等待,等待消费者将标识位设置位True
    event.wait()
    # 生产者开始工作后,要将标识为清空,即设置位False,让消费者处于等待状态
    event.clear()
    print "生产者:开始生产包子"
    time.sleep(5)
    print "生产者:包子生产完成"
    # 生产完成,将标识位设置位True,让消费者不处于等待状态
    event.set()

def consumer():
    print "消费者:还未到来..."
    time.sleep(3)
    print "消费者:来了..."
    # 将标识位设置位True,让消费者开始工作
    event.set()
    print "消费者:我要包子"
    # 这里需要延时
    time.sleep(1)
    # 通过判断生产者是否将标识位设置位True
    while True:
        if event.isSet():
            print "消费者:谢谢"
            break
        else:
            print "消费者:好了吗"
            time.sleep(1)

if __name__ == "__main__":

    #定义生产者和消费者的通信事件
    event = threading.Event()

    producer = threading.Thread(target=producer, args=())
    producer.start()

    consumer = threading.Thread(target=consumer, args=())
    consumer.start()

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/day23/thread_set_test.py
生产者:正在等待消费者到来...
消费者:还未到来...
消费者:来了...
消费者:我要包子生产者:开始生产包子

消费者:好了吗
消费者:好了吗
消费者:好了吗
消费者:好了吗
生产者:包子生产完成
消费者:谢谢

Process finished with exit code 0

注意:

(1)该通信事件的目的是生产者可以控制消费者是否等待;消费者也可以控制生产者是否等待