python多进程,多线程和协程的用法

一,用进程池的方式批量创建子进程

# 如果要启动大量的子进程,可以用进程池的方式批量创建子进程:
# 对Pool对象调用join()方法会等待所有子进程执行完毕,
# 调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了。

from multiprocessing import Pool
import os, time, random

def long_time_task(name):
    print('Run task %s (%s)...' % (name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print('Task %s runs %0.2f seconds.' % (name, (end - start)))

if __name__=='__main__':
    print('Parent process %s.' % os.getpid())
    p = Pool(4)
    for i in range(5):
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

二,多进程间通信

# Process之间肯定是需要通信的,操作系统提供了很多机制来实现进程间的通信。Python的multiprocessing模块包装了
# 底层的机制,提供了Queue、Pipes等多种方式来交换数据。
# 我们以Queue为例,在父进程中创建两个子进程,一个往Queue里写数据,一个从Queue里读数据:

from multiprocessing import Process, Queue
import os, time, random

# 写数据进程执行的代码:
def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
        q.put(value)
        time.sleep(random.random())

# 读数据进程执行的代码:
def read(q):
    print('Process to read: %s' % os.getpid())
    for i in range(3):
        value = q.get(True)
        print('Get %s from queue.' % value)
        time.sleep(random.random())

if __name__=='__main__':
    # 父进程创建Queue,并传给各个子进程:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 启动子进程pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束:
    pw.join()
    pr.join()
    print('All subprocesses done.')

三,多线程

# 该章节主要讲的是多线程和多线程锁的应用。
# 1,当多个线程同时执行lock.acquire()时,只有一个线程能成功地获取锁,然后继续执行代码,其他线程就继续等待
# 直到获得锁为止。
# 2,获得锁的线程用完后一定要释放锁,否则那些苦苦等待锁的线程将永远等待下去,成为死线程。
# 所以我们用try...finally来确保锁一定会被释放。
# 3,缺点,首先是阻止了多线程并发执行,包含锁的某段代码实际上只能以单线程模式执行,效率就大大地下降了。
# 其次,由于可以存在多个锁,不同的线程持有不同的锁,并试图获取对方持有的锁时,可能会造成死锁,
# 导致多个线程全部挂起,既不能执行,也无法结束,只能靠操作系统强制终止。

import time, threading

# 新线程执行的代码:
def loop():
    #获取线程锁
    lock.acquire()
    try:
        print('thread %s is running...' % threading.current_thread().name)
        n = 0
        while n < 5:
            n = n + 1
            print('thread %s >>> %s' % (threading.current_thread().name, n))
            time.sleep(1)
        print('thread %s ended.' % threading.current_thread().name)
    finally:
        # 改完了一定要释放锁:
        lock.release()

def hoop():
    #获取线程锁
    lock.acquire()
    try:
        print('thread %s is running...' % threading.current_thread().name)
        n = 0
        while n < 5:
            n = n + 1
            print('thread %s >>> %s' % (threading.current_thread().name, n))
            time.sleep(1)
        print('thread %s ended.' % threading.current_thread().name)
    finally:
        # 改完了一定要释放锁:
        lock.release()

def doop():
    print('thread %s is running...' % threading.current_thread().name)
    n = 0
    while n < 5:
        n = n + 1
        print('thread %s >>> %s' % (threading.current_thread().name, n))
        time.sleep(1)
    print('thread %s ended.' % threading.current_thread().name)


#创建一个锁
lock = threading.Lock()
print('thread %s is running...' % threading.current_thread().name)
t1 = threading.Thread(target=loop, name='LoopThread')
t2 = threading.Thread(target=hoop, name='HoopThread')
t3 = threading.Thread(target=doop, name='DoopThread')
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
print('thread %s ended.' % threading.current_thread().name)

#获取同一把锁的线程需要等另外一个线程处理完毕,才开始处理,没有上锁的线程,会同步执行

四,协程

# asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持。
# asyncio的编程模型就是一个消息循环。我们从asyncio模块中直接获取一个EventLoop的引用,
# 然后把需要执行的协程扔到EventLoop中执行,就实现了异步IO。

import threading
import asyncio

# hello()会首先打印出Hello world!,然后,yield from语法可以让我们方便地调用另一个generator。
# 由于asyncio.sleep()也是一个coroutine,所以线程不会等待asyncio.sleep(),而是直接中断并执行下一个消息循环
# 当asyncio.sleep()返回时,线程就可以从yield from拿到返回值(此处是None),然后接着执行下一行语句。

# @asyncio.coroutine把一个generator标记为coroutine类型,然后,我们就把这个coroutine扔到EventLoop中执行。
# @asyncio.coroutine
# def hello1():
#     print('Hello world1! (%s)' % threading.currentThread())
#     #如果把asyncio.sleep()换成真正的IO操作,则多个coroutine就可以由一个线程并发执行。
#     yield from asyncio.sleep(1)
#     print('Hello again1! (%s)' % threading.currentThread())

# @asyncio.coroutine
# def hello2():
#     print('Hello world2! (%s)' % threading.currentThread())
#     yield from asyncio.sleep(1)
#     print('Hello again2! (%s)' % threading.currentThread())

# if __name__=='__main__':
#     loop = asyncio.get_event_loop()
#     tasks = [hello1(), hello2()]
#     loop.run_until_complete(asyncio.wait(tasks))
#     loop.close()


# 为了简化并更好地标识异步IO,从Python 3.5开始引入了新的语法async和await,可以让coroutine的代码更简洁易读
# 请注意,async和await是针对coroutine的新语法,要使用新的语法,只需要做两步简单的替换:
# 把@asyncio.coroutine替换为async;
# 把yield from替换为await。

async def hello1():
    print('Hello world1! (%s)' % threading.currentThread())
    #如果把asyncio.sleep()换成真正的IO操作,则多个coroutine就可以由一个线程并发执行。
    await asyncio.sleep(1)
    print('Hello again1! (%s)' % threading.currentThread())

async def hello2():
    print('Hello world2! (%s)' % threading.currentThread())
    await asyncio.sleep(1)
    print('Hello again2! (%s)' % threading.currentThread())

if __name__=='__main__':
    loop = asyncio.get_event_loop()
    tasks = [hello1(), hello2()]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()

五,利用死循环验证python的GIL锁

# 我们可以监控到一个死循环线程会100%占用一个CPU。
# 如果有两个死循环线程,在多核CPU中,可以监控到会占用200%的CPU,也就是占用两个CPU核心。
# 要想把N核CPU的核心全部跑满,就必须启动N个死循环线程。
# 但是启动与CPU核心数量相同的N个线程,在4核CPU上可以监控到CPU占用率仅有100%,也就是仅使用了一核。

# 因为Python的线程虽然是真正的线程,但解释器执行代码时,有一个GIL锁:Global Interpreter Lock,
# 任何Python线程执行前,必须先获得GIL锁,然后,每执行100条字节码,解释器就自动释放GIL锁,
# 让别的线程有机会执行。这个GIL全局锁实际上把所有线程的执行代码都给上了锁,所以,多线程在Python中只能交替执行,
# 使100个线程跑在100核CPU上,也只能用到1个核。
# 所以,在Python中,可以使用多线程,但不要指望能有效利用多核。如果一定要通过多线程利用多核,
# 那只能通过C扩展来实现,不过这样就失去了Python简单易用的特点。
# 不过,也不用过于担心,Python虽然不能利用多线程实现多核任务,但可以通过多进程实现多核任务。
# 多个Python进程有各自独立的GIL锁,互不影响。

import threading, multiprocessing

def loop():
    x = 0
    while True:
        x = x+1
        print (x)

for i in range(multiprocessing.cpu_count()):        #启动与CPU核心数量相同的N个线程
    t = threading.Thread(target=loop)
    t.start()

以上就是近期总结的python实现高并发的一些用法,所有代码都是亲测有效,大家要是还有更好的写法,欢迎分享。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值