python在map中使用print_为什么`print`在Python多处理pool.map中不起作用

在Python 2.7中使用多处理模块处理大型CSV文件时,发现`print`语句在worker函数内不工作。代码示例中,`print(chunk)`和`print 'working'`并未输出,导致调试困难。问题在于`pool.map`不会直接执行包含`print`的函数,可能由于进程间通信的原因。解决方法通常包括使用日志记录或者返回信息到主进程进行打印。
摘要由CSDN通过智能技术生成

我正在尝试实现多处理模块以使用大型csv文件.我正在使用

Python 2.7并遵循

here的示例.

我运行了未经修改的代码(为方便起见,下面复制了代码)并注意到worker函数中的print语句不起作用.无法打印使得难以理解流程和调试.

任何人都可以解释为什么打印不在这里工作? pool.map不执行打印命令吗?我在网上搜索但没有找到任何表明这一点的文件.

import multiprocessing as mp

import itertools

import time

import csv

def worker(chunk):

# `chunk` will be a list of CSV rows all with the same name column

# replace this with your real computation

print(chunk) #

print 'working' #

return len(chunk)

def keyfunc(row):

# `row` is one row of the CSV file.

# replace this with the name column.

return row[0]

def main():

pool = mp.Pool()

largefile = 'test.dat'

num_chunks = 10

results = []

with open(largefile) as f:

reader = csv.reader(f)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值