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

python 函数 开启多线程示例

zhangchap3年前 (2021-05-19)日记本313
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 随机生成时间戳写入txt文件/运行sql语句

import time from random import randint with open('time.txt', ...

python url.parse模块编码解码

from urllib.parse import quote,unquote,urlencode # 对汉字进行编码使用 quote ...

python函数开启多线程

from threading import Thread 以下是代码举例: def main(num):    &nbs...

Python 正则表达式 带分组的替换 \g

import re re.sub(r'([^a-z]*)[a-z]([^a-z]*)', '\g<1>\g<2>',wor...

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

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

发表评论

访客

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