Python多线程线程池简单使用

对于高IO的程序,使用多线程可以明显提升性能,以下是Python多线程的简单示例:

#!/usr/bin/python
#coding:utf-8
'''
author:[email protected]
date:2017-09-06
version:1.0
python:3.x
description:multi-Process
'''

from multiprocessing.dummy import Pool as ThreadPool
import os
import threading

# 线程要执行的代码
def run_proc(data):
    print('Run thread element:%s,PID:%s ThreadId:%s.' % (data, os.getpid(),threading.currentThread().ident))

if __name__=='__main__':
    data=[]
    for i in range(1,1000):
        data.append(i)
    print('thread will start.')
    pool = ThreadPool(4)  
    pool.map(run_proc, data)
    pool.close()
    pool.join()
    print('thread process finished.')

你可能感兴趣的:(一些感想)