概念:https://www.liaoxuefeng.com/wiki/1016959663602400/1017629247922688
示例:https://blog.csdn.net/will130/article/details/50599577
1、多线程分别读取写入多个文件
#! /usr/bin/env python
#coding=utf-8
from threading import Thread
import threading
import time
import os
file_dict = '/Users/Downloads'
#多线程分别读取和写入多个文件
def file_io(thread_index):
outfile_path = file_dict + '/thread_output_' + str(thread_index) + '.txt'
outfile = open(outfile_path, 'w')
infile_path = file_dict + '/thread_input_' + str(thread_index) + '.txt'
with open(infile_path, 'r') as infile:
line = infile.readline()
while line:
outfile.write(line)
#print(line)
line = infile.readline()
outfile.close()
print(threading.current_thread().name + ': write schedule_times finish!')
def loop():
thread_index = int(threading.current_thread().name.split('_')[1])
print('thread %s is running...' % threading.current_thread().name)
file_io(thread_index)
if __name__ == '__main__':
for i in range(3):
Thread(target=loop, name='thread_' + str(i), args=()).start()