#!/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()
多线程实现文档处理
最新推荐文章于 2023-05-12 17:44:28 发布