1. 程式人生 > >多執行緒與多程序方法對比

多執行緒與多程序方法對比

多執行緒與多程序方法對比

多執行緒與多程序將0-10000數字寫入csv檔案,對花費時間進行對比
完成程式碼獲取地址:https://download.csdn.net/download/luzaofa/10908723
1、普通插入與程序池類比結果:

在這裡插入圖片描述

程式碼如下:

# Time    : 12/27/2018 9:53 AM
# Author  : Luzaofa

import time
import types
import copy_reg
import multiprocessing as mp


def _pickle_method(m):
    if m.im_self is None:
        return getattr, (m.im_class, m.im_func.func_name)
    else:
        return getattr, (m.im_self, m.im_func.func_name)


copy_reg.pickle(types.MethodType, _pickle_method)


class Demo(object):

    def __init__(self):
        pass

    def test(self, mass):
        '''資訊寫入檔案'''
        with open('test.csv', 'a+') as f:
            f.writelines(str(mass) + '\n')

    def data_mp(self, func, mass):
        '''程序池'''
        pool = mp.Pool(processes=4)
        for i in mass:
            pool.apply_async(func, args=(i,))
        pool.close()
        pool.join()

    def pb_main(self):
        '''普通資料插入'''
        start = time.time()
        for i in range(10000):
            self.test(i)
        end = time.time()
        print('總用時間:%s' % (end - start))

    def mp_main(self):
        '''程序池插入'''
        start = time.time()
        self.data_mp(self.test, [i for i in range(10000)])
        end = time.time()
        print('總用時間:%s' % (end - start))


if __name__ == '__main__':
    demo = Demo()
    demo.pb_main()
    print '-----------------'
    demo.mp_main()

2、普通插入與多執行緒類比結果:

在這裡插入圖片描述

程式碼如下:

# Time    : 12/27/2018 9:53 AM
# Author  : Luzaofa

import time
import threading


class MyThread(threading.Thread):

    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args

    def run(self):
        apply(self.func, self.args)


class Demo(object):

    def __init__(self):
        self.mass = [i for i in range(10000)]

    def test(self, mass):
        '''資訊寫入檔案'''
        with open('test.csv', 'a+') as f:
            f.write(str(mass) + '\n')
            f.flush()

    def pb_main(self):
        '''普通資料插入'''
        start = time.time()
        for i in range(10000):
            self.test(i)
        end = time.time()
        print('總用時間:%s' % (end - start))

    def td_main(self):
        '''多執行緒'''
        start = time.time()
        threads = []
        for i in self.mass:
            t = MyThread(self.test, (i,), self.test.__name__)
            threads.append(t)
        for i in self.mass:
            threads[i].start()
        for i in self.mass:
            threads[i].join()
        end = time.time()
        print('總用時間:%s' % (end - start))


if __name__ == '__main__':
    demo = Demo()
    demo.pb_main()
    print '----------------------'
    demo.td_main()

結論:程序池適合cpu密集型操作、多執行緒適合於IO密集型操作