python 函数 开启多线程示例
from threading import Thread
def readfile(queue:Queue):
curdir = os.path.dirname(__file__)
article_path = os.path.join(curdir, 'article')
print(article_path)
articles = os.listdir(article_path)
os.chdir(article_path)
for article in articles:
with open(article, encoding='utf-8') as f:
title = article.split('-')[0]
if not title:
print(article)
continue
pubtime = article.split('-')[-1]
content = f.read()
queue.put((title,content,pubtime))
if __name__ == '__main__':
article_queue = Queue()
rf = Thread(target=readfile,args=(article_queue,)) # 注意括号后面是逗号
rf.daemon = True
rf.start()