当前位置:首页 > 日记本 > 正文内容

python 函数 开启多线程示例

zhangchap3年前 (2021-05-19)日记本314
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()


标签: python笔记
分享给朋友:

相关文章

python使用mongodb数据库

from pymongo import MongoClient,collection class KSpdier(Thread):   ...

python下random随机选择的三种方式

from random import sample,choice,choices list_1 = [1,2,3,4,5,6] # 从列...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。