ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 低调看直播体育app软件下载 >> Pythonjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播 >> python多线程 Python(八)进程、线程、协程篇

python多线程 Python(八)进程、线程、协程篇(2/6)

来源:网络整理     时间:2016-06-27     关键词:python多线程

本篇文章主要介绍了"python多线程 Python(八)进程、线程、协程篇",主要涉及到python多线程方面的内容,对于Pythonjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 本章内容:线程(线程锁、threading.Event、queue 队列、生产者消费者模型、自定义线程池)进程(数据共享、进程池)协程线程Threading用于...

Python提供的Condition对象提供了对复杂线程同步问题的支持。Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还提供了wait和notify方法。线程首先acquire一个条件变量,然后判断一些条件。如果条件不满足则wait;如果条件满足,进行一些处理改变条件后,通过notify方法通知其他线程,其他处于wait状态的线程接到通知后会重新判断条件。不断的重复这一过程,从而解决复杂的同步问题。

在典型的设计风格里,利用condition变量用锁去通许访问一些共享状态,线程在获取到它想得到的状态前,会反复调用wait()。修改状态的线程在他们状态改变时调用 notify() or notify_all(),用这种方式,线程会尽可能的获取到想要的一个等待者状态。

import threading
import time
def consumer(cond): with cond: print("consumer before wait") cond.wait() print("consumer after wait") def producer(cond): with cond: print("producer before notifyAll") cond.notifyAll() print("producer after notifyAll") condition = threading.Condition() c1 = threading.Thread(name="c1", target=consumer, args=(condition,)) c2 = threading.Thread(name="c2", target=consumer, args=(condition,)) p = threading.Thread(name="p", target=producer, args=(condition,)) c1.start() time.sleep(2) c2.start() time.sleep(2) p.start() # consumer()线程要等待producer()设置了Condition之后才能继续。

queue 队列

适用于多线程编程的先进先出数据结构,可以用来安全的传递多线程信息。

queue 方法:

  • q = queue.Queue(maxsize=0) # 构造一个先进显出队列,maxsize指定队列长度,为0 时,表示队列长度无限制。
  • q.join()   # 等到队列为kong的时候,在执行别的操作
  • q.qsize()   # 返回队列的大小 (不可靠)
  • q.empty()    # 当队列为空的时候,返回True 否则返回False (不可靠)
  • q.full()     # 当队列满的时候,返回True,否则返回False (不可靠)
  • q.put(item, block=True, timeout=None) # 将item放入Queue尾部,item必须存在,可以参数block默认为True,表示当队列满时,会等待队列给出可用位置,为False时为非阻塞,此时如果队列已满,会引发queue.Full 异常。 可选参数timeout,表示 会阻塞设置的时间,过后,如果队列无法给出放入item的位置,则引发 queue.Full 异常
  • q.get(block=True, timeout=None) # 移除并返回队列头部的一个值,可选参数block默认为True,表示获取值的时候,如果队列为空,则阻塞,为False时,不阻塞,若此时队列为空,则引发 queue.Empty异常。 可选参数timeout,表示会阻塞设置的时候,过后,如果队列为空,则引发Empty异常。
  • q.put_nowait(item) # 等效于 put(item,block=False)
  • q.get_nowait()     # 等效于 get(item,block=False)

生产者消费者模型

import queue
import threading

que = queue.Queue(10)

def s(i):
    que.put(i)
    # print("size:", que.qsize())

def x(i):
    g = que.get(i)
    print("get:", g)

for i in range(1, 13):
    t = threading.Thread(target=s, args=(i,))
    t.start()

for i in range(1, 11):
    t = threading.Thread(target=x, args=(i,))
    t.start()

print("size:", que.qsize())

# 输出结果:
get: 1
get: 2
get: 3
get: 4
get: 5
get: 6
get: 7
get: 8
get: 9
get: 10
size: 2

自定义线程池:

 Python(八)进程、线程、协程篇 Python(八)进程、线程、协程篇

# 自定义线程池(一)import queue
import threading
import time

class TreadPool:

    def__init__(self, max_num=20):
        self.queue = queue.Queue(max_num)
        for i in range(max_num):
            self.queue.put(threading.Thread)

    def get_thread(self):
        return self.queue.get()

    def add_thread(self):
        self.queue.put(threading.Thread)

def func(pool, n):
    time.sleep(1)
    print(n)
    pool.add_thread()

p = TreadPool(10)
for i in range(1, 100):
    thread = p.get_thread()
    t = thread(target=func, args=(p, i,))
    t.start()
自定义线程池(一)

 Python(八)进程、线程、协程篇 Python(八)进程、线程、协程篇

# 线程池(二)import queue
import threading
import contextlib
import time

StopEvent = object()

class Threadpool:

    def__init__(self, max_num=10):
        self.q = queue.Queue()
        self.max_num = max_num

        self.terminal = False
        self.generate_list = []     # 以创建线程列表        self.free_list = []         # 以创建的线程空闲列表def run(self, func, args, callback=None):
        """        线程池执行一个任务
        :param func: 任务函数
        :param args: 任务函数所需参数
        :param callback: 任务执行失败或成功后执行的回调函数,回调函数有两个参数1、任务函数执行状态;2、任务函数返回值(默认为None,即:不执行回调函数)
        :return: 如果线程池已经终止,则返回True否则None
        """if len(self.free_list) == 0 and len(self.generate_list) < self.max_num:
            self.generate_thread()
        w = (func, args, callback,)
        self.q.put(w)

    def generate_thread(self):
        """        创建一个线程
        """        t = threading.Thread(target=self.call)
        t.start()

    def call(self):
        """        循环去获取任务函数并执行任务函数
        """        current_thread = threading.currentThread    # 当前线程        self.generate_list.append(current_thread)

        event = self.q.get()
        while event != StopEvent:

            func, arguments, callback = event
            try:
                result = func(*arguments)
                status = True
            except Exception as e:
                status = False
                result = e

            if callback isnot None:
                try:
                    callback(status, result)
                except Exception as e:
                    passif self.terminal:
                event = StopEvent
            else:
                with self.worker_state(self.free_list, current_thread):
                    event = self.q.get()
                # self.free_list.append(current_thread)# event = self.q.get()# self.free_list.remove(current_thread)else:
            self.generate_list.remove(current_thread)

    def close(self):
        """        执行完所有的任务后,所有线程停止
        """        num = len(self.generate_list)
        while num:
            self.q.put(StopEvent)
            num -= 1

    def terminate(self):
        """        无论是否还有任务,终止线程
        """        self.terminal = True
        while self.generate_list:
            self.q.put(StopEvent)
        self.q.empty()  # 清空队列    @contextlib.contextmanager      # with上下文管理def worker_state(self, frelist, val):
        """        用于记录线程中正在等待的线程数
        """        frelist.append(val)
        try:
            yieldfinally:
            frelist.remove(val)

def work(i):
    time.sleep(1)
    print(i)

pool = Threadpool()
for item in range(50):
    pool.run(func=work, args=(item,))
pool.close()
# pool.terminate()
自定义线程池(二)
进程

# 进程
from multiprocessing import Process

def work(name):
    print("Hello, %s" % name)

if __name__ == "__main__":
    p = Process(target=work, args=("nick",))
    p.start()
    p.join()

注意:由于进程之间的数据需要各自持有一份,所以创建进程需要的非常大的开销。

数据共享

不同进程间内存是不共享的,要想实现两个进程间的数据交换,可以用以下方法:

Shared memory

数据可以用Value或Array存储在一个共享内存地图里,如下:

from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print(num.value)
    print(arr[:])

# 输出:
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

创建num和arr时,“d”和“i”参数由Array模块使用的typecodes创建:“d”表示一个双精度的浮点数,“i”表示一个有符号的整数,这些共享对象将被线程安全的处理。

python多线程相关图片

python多线程相关文章