python 两种多线程比较

枫铃3年前 (2021-09-30)Python220

首先我是为了把这56w左右的数据清洗
在这里插入图片描述
变成这样:
在这里插入图片描述
从一个txt清洗,写到另一个txt中。 原本是几千条数据 ,一直用的普通的,速度还挺快,今天想清洗这56w数据,就想到了多线程 。

第一种方法:

def huoqu(file):
    ts_queue = Queue(10000)
    with open(file, 'r')as f:
        t = f.read()
        IP = t.split('\n')
        for i in IP:
            ts_queue.put(i)
        return ts_queue
def qingxi(ts_queue):
    while not ts_queue.empty():
        i = ts_queue.get()
        port_1 = re.findall(r"W12.*", i)
        port_1 = ''.join(port_1)
        try:
            t = zidian.zi_dian()
            port = str(t[port_1])
        except:
            port = '9999'
        port_2 = re.findall(r"/common.*", i)
        port_2 = ''.join(port_2)
        IP = i.replace(port_2, port)
        with open('IP3.txt', 'a+')as g:
            g.write(IP)
            g.write('\n')

with open('IP.txt','r')as f:
    t= f.read()
    IP = t.split('\n')
    heji = []
    for i in IP:
        port_1 = re.findall(r"W12.*", i)
        port_1 = ''.join(port_1)
        try:
            t = zidian.zi_dian()
            port = str(t[port_1])
        except:
            port = '9999'
        port_2 = re.findall(r"/common.*", i)
        port_2 = ''.join(port_2)
        IP = i.replace(port_2, port)
        heji.append(IP)
        #print(IP)
    heji.pop()
    for i in heji:
        with open('IP2.txt', 'a+')as g:
            g.write(i)
            g.write('\n')


if __name__ == "__main__":

    start = datetime.datetime.now().replace(microsecond=0)
    print('开始————————读取列表:')
    t = 'IP.txt'
    s = huoqu(t)
    threads = []
    for i in range(100):
        t = threading.Thread(target=qingxi, name='th-' + str(i), kwargs={'ts_queue': s})
        threads.append(t)
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    end = datetime.datetime.now().replace(microsecond=0)
    print('删除耗时:' + str(end - start))

这是平常我最喜欢用的多线程方法,非阻塞式得,速度最快的,但今天卡死了,不动了,原因

return ts_queue
需要添加56w的数据加入,来进行线程运作,当时不行了。
不添加线程还能运作,就是很慢,这个完全不工作了、

想到了换下一种,并且运用了自己带函数 map() 来提高效率。

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def square(x):
    port_1 = re.findall(r"W12.*", x)
    port_1 = ''.join(port_1)
    try:
        t = zidian.zi_dian()
        port = str(t[port_1])
    except:
        port = '9999'
    port_2 = re.findall(r"/common.*", x)
    port_2 = ''.join(port_2)
    IP = x.replace(port_2, port)
    return IP
def main():
    with open('IP.txt', 'r')as f:
        t = f.read()
        IP = t.split('\n')
        IP.pop()
        res = map(square, IP)
        t_list = []
        for ip_port in res:
            t = threading.Thread(target=is_enable, args=(ip_port,))
            t.start()
            t_list.append(t)
        for t in t_list:
            t.join()


def is_enable(ip_port):
    with open('IP3.txt', 'a+')as g:
        g.write(ip_port)
        g.write('\n')

if __name__ == '__main__':
    start = datetime.datetime.now().replace(microsecond=0)
    main()
    end = datetime.datetime.now().replace(microsecond=0)
    print('删除耗时:' + str(end - start))
    #删除耗时:0:05:14

并换上了另一种快速的多线程,清洗用内置函数完成,写入文件用多线程,但居然用了5分多种,多了几个for循环,大大拉低了速度,这就说明这个完全没必要用多线程,还拉低了速度。

这时候看下不用多线程的。

def square(x):
    port_1 = re.findall(r"W12.*", x)
    port_1 = ''.join(port_1)
    try:
        t = zidian.zi_dian()
        port = str(t[port_1])
    except:
        port = '9999'
    port_2 = re.findall(r"/common.*", x)
    port_2 = ''.join(port_2)
    IP = x.replace(port_2, port)
    return IP
start = datetime.datetime.now().replace(microsecond=0)
with open('IP.txt', 'r')as f:
    t = f.read()
    IP = t.split('\n')
    IP.pop()
    res = map(square, IP)
    for i in res:
        with open('IP3.txt', 'a+')as g:
            g.write(i)
            g.write('\n')
    end = datetime.datetime.now().replace(microsecond=0)
    print('删除耗时:' + str(end - start))
    # 删除耗时:0:03:52

明显快多了,只用4分钟左右,显然for 循环在56w数据面前,大大拉低了速度,耗费了时间,所以两种多线程个有优点,当数据过大,写入文件不如 不用多线程。

要想加快,可以把列表分成几个,单独给每个列表写入文件,但顺序会发生变化,更加吃电脑配置了。

相关文章

利用python同步windows和linux文件

写python脚本的初衷,每次在windows编辑完文件后,想同步到linux上去,只能够登录服务器,...

爬虫基本原理

爬虫基本原理 一、爬虫是什么? 百度百科和维基百科对网络爬虫的定义:简单来说爬虫就是抓取目标网站内容的工具,一般是根据定义的行...

Django 函数和方法的区别

函数和方法的区别 1、函数要手动传self,方法不用传 2、如果是一个函数,用类名去调用,如果是一个方法...

Django 知识补漏单例模式

单例模式:(说白了就是)创建一个类的实例。在 Python 中,我们可以用多种方法来实现单例模式&#x...

Django基础知识MTV

Django简介 Django是使用Python编写的一个开源Web框架。可以用它来快速搭建一个高性能的网站。 Django也是一个MVC框架。但是在Dj...

Python mysql 索引原理与慢查询优化

一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,...

发表评论

访客

看不清,换一张

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