簡體   English   中英

使用Python多處理庫的奇怪行為

[英]Strange behavior using Python multiprocessing library

我正在嘗試使用python multiprocessing庫讀取文件,但未獲得所需的結果。 這里是我正在使用的代碼:

import multiprocessing as mp
import itertools

partitioned = {}
partitioned['0-20'] = []
partitioned['20-40'] = []
partitioned['40-60'] = []
partitioned['60+'] = []
output = []

def map_func1(f):
    # for line in f:
    gen = f[14:15] #15 1=male 2=female
    age = f[17:19] #18-19
    htin = f[1947:1950] #1948-1950 tall in inches, self reported !888! !999!
    wtlbs = f[1950:1953] #1951-1953 wt in lbs, self reported !888! !999!
    ovwt = f[1963:1964] #1964 consider myself overweight 1,under 2,over 3, !8!, !9!
    chwt = f[1964:1965] #1965 change weight or stay same 1=more, 2=less, 3=same, !8!, !9!
    output.append([gen, age, htin, wtlbs, ovwt, chwt])
    return output

def partitioner(m):
    for element in m:
        if int(element[1]) < 20:
            output['0-20'].append(element)
        elif int(element[1]) < 40:
            output['20-40'].append(element)
        elif int(element[1]) < 60:
            output['40-60'].append(element)
        else:
            output['60+'].append(element)

    return partitioned

if __name__ == "__main__":
    pool = mp.Pool(processes=3)
    f = open('adult.dat')
    m = pool.map(map_func1, f)
    print len(output)
    print len(m)
    p = partitioner(m)
    print p

這是我收到的輸出:

TypeError: int() argument must be a string or a number, not 'list'
0
20050

我有以下問題:

  1. 我不明白為什么在上述代碼中, output的長度為0而變量m的長度為20050。據我說, outputm的長度都應為20050。

  2. 為什么在這種情況下是TypeError() 為什么參數不能是partitioner函數中的列表?

  3. 當我嘗試在調試窗口中查看變量m的內容時,我的系統幾乎崩潰了。 (我正在使用Ubuntu 13.10,並在其上運行Pycharm 3.1!)如果我要查看的列表的內容異常龐大,那么我可以理解這一點,在這種情況下,它們不是。 它是20050列表的列表,每個列表包含6個元素。

在這方面的任何幫助將不勝感激。

只是為了解決您的錯誤, partitioner調用:

int(element[1])

但是, map_func1element1age ,它定義為:

age = f[17:19] #18-19

這是一個包含兩個項目的列表片,本身就是一個列表,因此不是int的有效參數。

對於其他人,我建議您輸出一個樣本以查看其中的內容,例如

print m[:5]

問題是我沒有從映射器函數正確返回內容。 代碼中的微小更改使其可以按要求工作:

import multiprocessing as mp
import itertools

partitioned = {}
partitioned['0-20'] = []
partitioned['20-40'] = []
partitioned['40-60'] = []
partitioned['60+'] = []

def map_func1(f):
    # for line in f:
    gen = f[14:15] #15 1=male 2=female
    age = f[17:19] #18-19
    htin = f[1947:1950] #1948-1950 tall in inches, self reported !888! !999!
    wtlbs = f[1950:1953] #1951-1953 wt in lbs, self reported !888! !999!
    ovwt = f[1963:1964] #1964 consider myself overweight 1,under 2,over 3, !8!, !9!
    chwt = f[1964:1965] #1965 change weight or stay same 1=more, 2=less, 3=same, !8!, !9!
    return [gen, age, htin, wtlbs, ovwt, chwt]

def partitioner(m):
    for element in m:
        if int(element[1]) < 20:
            partitioned['0-20'].append(element)
        elif int(element[1]) < 40:
            partitioned['20-40'].append(element)
        elif int(element[1]) < 60:
            partitioned['40-60'].append(element)
        else:
            partitioned['60+'].append(element)

    return partitioned

if __name__ == "__main__":
    pool = mp.Pool(processes=3)
    f = open('adult.dat')
    m = pool.map(map_func1, f)
    print m[0]
    p = partitioner(m)
    print len(p['60+'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM