多线程实现文档处理

#!/usr/bin/pytho3.7
# -*- coding: utf-8 -*-

import os
import sys
import csv
import time
import threading
from queue import Queue, LifoQueue, PriorityQueue

path1 = r'柜面业务数据表.csv'
path2 = r"柜面业务数据表1.dat"

q = Queue()
lock = threading.Lock()

def change_sep(path):
    with open(path, 'r', encoding='utf-8') as fr:
        reader = csv.reader(fr)
        for i in reader:
            # print(i)   type is list
            new_i = ''
            for data in i[1:]:
                new_i += str(data) + '|@|'
            q.put(new_i)
        # print(q.queue)
        # print(len(q.queue))


def save_str_to_dat(path):
    with open(path, 'a+', encoding='utf-8') as fw:
        while True:
            if lock.acquire():
                str1 = q.get()
                print(str1)

                fw.write(str1 + '\n')
                lock.release()
            if q.empty() == True:
                break
        q.task_done()


time1 = time.time()
if os.path.exists(path2):
    print('当前文件已经存在,删除之后,在进行插入')
    os.remove(path2)

print('生产者线程1启动------------------')
# 多线程传参方式一
t1 = threading.Thread(target=change_sep, name=1, args=(path1, ))
# 多线程传参方式二
# t1 = threading.Thread(target=change_sep, name=1, kwargs={'path': path1})
t1.start()

# 循环创建多线程
for i in range(2):
    t = threading.Thread(target=save_str_to_dat, name=i, kwargs={'path': path2})
    print('消费者线程{}启动------------------'.format(i))
    t.start()
    t.join()
time2 = time.time()
print(u'总共耗时:' + str(time2 - time1) + 's')


# print('消费者线程1启动------------------')
# t3 = threading.Thread(target=save_str_to_dat, name=2, kwargs={'path': path2})
# t3.start()
# print('消费者线程2启动------------------')
# t4 = threading.Thread(target=save_str_to_dat, name=3, kwargs={'path': path2})
# t4.start()
# print('消费者线程3启动------------------')
# t5 = threading.Thread(target=save_str_to_dat, name=4, kwargs={'path': path2})
# t5.start()
# time2 = time.time()
# print(u'总共耗时:' + str(time2 - time1) + 's')

# #生产消费模型
# q = Queue(maxsize=10)
#
# def product(name):
#     count = 1
#     while True:
#         q.put('步枪{}'.format(count))
#         print('{}生产步枪{}支'.format(name,count))
#         count+=1
#         time.sleep(0.3)
#         if count > 20:
#             break
#
# def cousume(name):
#     while True:
#         print('{}装备了{}'.format(name,q.get()))
#         time.sleep(0.3)
#
#         q.task_done()
#
#
# #部队线程
# p = threading.Thread(target=product,args=('张三',))
# k = threading.Thread(target=cousume,args=('李四',))
# w = threading.Thread(target=cousume,args=('王五',))
#
# p.start()
# k.start()
# w.start()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值