Python多进程和多线程示例

1.多进程(执行效率与CPU核数直接相关)

from multiprocessing import Pool, Manager
import os, time, random

#多进程执行的任务
def task(m_list):
    print('Run task pid %s...' % ( os.getpid()))
    for i in range(5):
        m_list.append(i)
    print(m_list)

if __name__=='__main__':
    start_time = time.time()
    print('Parent process %s.' % os.getpid())
    p = Pool()
    #Manager支持的类型有list,dict,Namespace,Lock,RLock,Semaphore,BoundedSemaphore,Condition,Event,Queue,Value和Array
    m = Manager()   # 创建共享list
    m_list = m.list()
    # for i in range(multiprocessing.cpu_count()):   # 根据CPU核数创建进程
    for i in range(5):
        p.apply_async(task, args=(m_list,))   # 注意:args中若为单个变量,必须以逗号','结尾
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')
    end_time = time.time()
    print('use time: {0}'.format(end_time - start_time))

2.多线程(python中的多线程由于GIL的原因,并不会提升效率)

import threading
import time

#线程
class ThreadImpl(threading.Thread):
    def __init__(self, num):
        threading.Thread.__init__(self)
        self._num = num

    def run(self):
        global total, mutex

        # 打印线程名
        print(threading.currentThread().getName())

        # 取得锁
        mutex.acquire()
        for x in range(0, int(self._num)):
            total = total + 1
            time.sleep(0.01)
            print(total)
        # 释放锁
        mutex.release()

if __name__ == '__main__':
    #定义全局变量
    global total, mutex
    total = 0
    # 创建锁
    mutex = threading.Lock()

    #定义线程池
    threads = []
    # 创建线程对象
    for x in range(4):
        threads.append(ThreadImpl(10))
    # 启动线程
    for t in threads:
        t.start()
    # 等待子线程结束
    for t in threads:
        t.join()

    # 打印执行结果
    print('total: {0}'.format(total))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xuerba

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

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

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

打赏作者

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

抵扣说明:

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

余额充值