Multithreading 如何使用多线程使python代码更快?

Multithreading 如何使用多线程使python代码更快?,multithreading,python-2.7,multiprocessing,Multithreading,Python 2.7,Multiprocessing,这是我调用的程序中唯一的函数,我传递的参数“apps”是directory,其中包含5000个json文件,我将在这些文件上做一些算术运算,处理所有文件大约需要25分钟。 现在,我想用多线程做同样的事情,让它更快。 谁能帮我一下吗 def directory(apps): for files in os.listdir(apps): files_path = os.path.join(apps, files) with open(files_path,

这是我调用的程序中唯一的函数,我传递的参数“apps”是directory,其中包含5000个json文件,我将在这些文件上做一些算术运算,处理所有文件大约需要25分钟。 现在,我想用多线程做同样的事情,让它更快。 谁能帮我一下吗

def directory(apps):
    for files in os.listdir(apps):
        files_path = os.path.join(apps, files) 
        with open(files_path, 'r') as data_file:

       #Doing some stuff



try:
    time.sleep(1)
    start_time = time.time()
    directory(application)
    print "===== %s seconds =====" % (time.time() - start_time)
except KeyboardInterrupt:
    print '\nInterrupted'
    print "===== %s seconds =====" % (time.time() - start_time)

首先看生成器,将目录函数拆分为一次只传递一个文件的函数。然后启动几个线程,分别调用目录,直到它的文件用完为止……打开文件后你在做什么?是CPU还是I/O绑定作业?我只是在验证文件中的一些数据。如果数据存在,我会将该数据保存在其他文件夹中。