python 多线程大文件_使用Python和多线程处理巨大的CSV文件

I have a function that yields lines from a huge CSV file lazily:

def get_next_line():

with open(sample_csv,'r') as f:

for line in f:

yield line

def do_long_operation(row):

print('Do some operation that takes a long time')

I need to use threads such that each record I get from the above function I can call do_long_operation.

Most places on Internet have examples like this, and I am not very sure if I am on the right path.

import threading

thread_list = []

for i in range(8):

t = threading.Thread(target=do_long_operation, args=(get_next_row from get_next_line))

thread_list.append(t)

for thread in thread_list:

thread.start()

for thread in thread_list:

thread.join()

My questions are:

How do I start only a finite number of threads, say 8?

How do I make sure that each of the threads will get a row from get_next_line?

解决方案

You could use a thread pool from multiprocessing and map your tasks to a pool of workers:

from multiprocessing.pool import ThreadPool as Pool

# from multiprocessing import Pool

from random import randint

from time import sleep

def process_line(l):

print l, "started"

sleep(randint(0, 3))

print l, "done"

def get_next_line():

with open("sample.csv", 'r') as f:

for line in f:

yield line

f = get_next_line()

t = Pool(processes=8)

for i in f:

t.map(process_line, (i,))

t.close()

t.join()

This will create eight workers and submit your lines to them, one by one. As soon as a process is "free", it will be allocated a new task.

There is a commented out import statement, too. If you comment out the ThreadPool and import Pool from multiprocessing instead, you will get subprocesses instead of threads, which may be more efficient in your case.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值