python 多进程和多线程

在计算大量数据时,可以使用多进程 多线程机制来加速计算

多进程

import multiprocessing
import os

def run_proc(name):
    print('Child process {0} {1} Running '.format(name, os.getpid()))

if __name__ == '__main__':
    print('Parent process {0} is Running'.format(os.getpid()))
    for i in range(5):
        p = multiprocessing.Process(target=run_proc, args=(str(i),))
        print('process start')
        p.start()
    p.join()
    print('Process close')

多线程

import time, threading

def run_thread(n,m):
        print(n,m)
        
if __name__ == '__main__':
    t1 = threading.Thread(target=run_thread, args=(5,6))
    t2 = threading.Thread(target=run_thread, args=(8,9))
    t1.start()
    t2.start()
    t1.join()
    t2.join()

猜你喜欢

转载自www.cnblogs.com/yeran/p/10448777.html