import time
from multiprocessing import Process, JoinableQueue, cpu_count
import csv ####处理一条数据的方法
def deal_line(line, writer, csv_file):
writer.writerow((line[3], line[1]))
csv_file.flush()#重点,在多进程中写文件需要尽快刷新,否则可能会导致数据丢失 ####消费者模型
def consumer(queue, writer, csv_file):
while True:
line = queue.get()
deal_line(line, writer, csv_file)
queue.task_done()
####生产者模型
def producer(queue):
with open('test.txt', 'r') as f:
for line in f:
queue.put(line) ####启动N个生产者N个消费者模型
def main():
with open('t1.csv', 'w+') as csv_file:
writer = csv.writer(csv_file)
queue = JoinableQueue(8)#可限制队列长度
pc = Process(target=producer, args=(queue,))
pc.start() #多消费者
for _ in range(cpu_count()):
c1 = Process(target=consumer, args=(queue, writer, csv_file))
c1.daemon = True
c1.start()
pc.join()#等待生产者进程全部生成完毕
queue.join()# 等待所有数据全部处理完毕 # 当某些些进程是死循环时可强制终止
# pc.terminate() if __name__ == '__main__': now = lambda: time.time()
start = now()
main()
print("Time:", now() - start)

  

python 多进程读写文件的更多相关文章

  1. Python 3 读写文件的简单方法!

    Python 3 读写文件的简单方法! a = open('test.txt','w') 这行代码创建了一个名为test的文本文档,模式是写入(模式分为三种,w代表写入,r代表阅读,a代表在尾行添加) ...

  2. 笨方法学python之读写文件、open函数的用法

    一.python读写文件相关知识点 close:关闭文件 read:读取文件的内容//你可以把结果赋给一个变量 readline:只读取文件中的一行 truncate 美 /trʌŋ'ket/ :清空 ...

  3. Python:读写文件(I/O) | 组织文件

    1. I/O 概述  程序与用户交互涉及到程序的输入输出(I/O) 一种类型是字符串,通过input() 和 print() 函数以及数据类型转换类函数如(int()),实现数据的输入输出. 另一种类 ...

  4. python查找读写文件

    import os ''' 跟据文件名称,后缀查找指定文件 path:传入的路径 filename:要查找的文件名 suffix:要查找的文件后缀 return :返回查找的文件路径 ''' file ...

  5. python之读写文件

    1. 读取文件数据,文件必须存在才可以读且如要读取的文件不和当前.py在同一个包下,需要特别指定此文件路径才行 f=open('test.txt',encoding='utf-8')#填写文件路径,打 ...

  6. Python 多进程拷贝文件夹案例

    import os import multiprocessing def copy_file(q, file_name, old_folder_name, new_folder_name): &quo ...

  7. 【python】 读写文件

    #标准输出 sys.stdout.write() sys.stderr.write() #标准输入 while True : try: line = raw_input().rstrip(); exc ...

  8. Python中读写文件三部曲

    写入文件:要把第二个参数 'r' 改成 'w' ,表示write,即以写入的模式打开文件;  往文件中写入内容,使用write()函数. 例子如下:注意 'w' 写入模式会暴力清空掉原有文件,然后再写 ...

  9. python 二进制读写文件

    #-*- coding: utf-8 -*- f = open('f:/text.bmp','rb') filedata = f.read() filesize = f.tell() f.close( ...

随机推荐

  1. 最长公共子序列(LCS) Easy

    A subsequence of a given sequence is the given sequence with some elements (possible none) left out. ...

  2. 搬家至csdn

    搬家至csdn https://blog.csdn.net/qq_42866164

  3. CentOS7.6下设置mysql服务开机启动

    在centos7中所有对服务的管理都集中到了systemctl当中,所以服务的启动.关闭.重启.开机启动等等的操作都可以用systemctl. systemctl对服务的管理都是通过配置文件,配置文件 ...

  4. Spring基础12——使用外部属性文件

    1.使用外部属性文件 在配置文件里配置Bean时,有时需要在Bean的配置文件里引入系统部署的细节信息(例如:文件的路径.数据源配置信息等),而这些部署细节实际上需要和bean配置相分离,因为我们修改 ...

  5. nroff - 用 groff 模拟 nroff 命令

    总览 (SYNOPSIS) nroff [ -h ] [ -i ] [ -mname ] [ -nnum ] [ -olist ] [ -rcn ] [ -Tname ] [ file... ] 描述 ...

  6. netstat - 显示网络连接,路由表,接口状态,伪装连接,网络链路信息和组播成员组。

    总览 SYNOPSIS netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--al ...

  7. Codeforces 950 010子序列拆分 数列跳棋

    A B a,b两个序列分成K段 每一段的值相等 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset ...

  8. linux局域网通过映射(双网卡)访问外网

    图示说明: 1.IP规划设置 主机名 ip地址 ip地址(第二个网卡配置的地址) 地址类别 oldboy01 192.168.10.20 空 仅可访问内网主机 oldboy02 192.168.10. ...

  9. 两个jquery编写插件实例

    (1) 封装基于jq弹窗插件   相信码友们对于$.fn.extexd();$.extend()以及$.fn.custom和$.custom都有一定的了解:我阐述一下我自己对于$.fn.custom和 ...

  10. AngualJS-leaflet之视图等级缩放

    在http://tombatossals.github.io/angular-leaflet-directive/#!/examples/events 中的则是zoomlevelschange,然后识 ...