0

こんにちは、私はサーバーからいくつかのデータを照会しており、見つけたデータをファイルに保存したいと考えています。プールとマルチプロセッシングを使用しない場合、データは必要に応じてファイルに書き込まれます。ここで何が間違っていますか?キューについて言及されましたが、適切に使用しているとは思えず、使用方法がわかりません。私はマルチプロセッシングに非常に慣れていません

最後の値だけがファイルに書き込まれているか、まったく書き込まれていないようです...

from multiprocessing import Pool
import time
import os
import json
import urllib2
import pprint

start_time = time.time()
localtime = time.localtime()
unixtime = int(time.mktime(time.gmtime()))

if not os.path.exists('data'):
    os.makedirs('data')
filename = ('data/%i_%i_%i_%i_%i_%i') % (localtime[2], localtime[1], localtime[0], localtime[3], localtime[4], localtime[5])
f = open(filename, 'w')

apikey = '_omitted_'

res = 90

lines = (360 * 180) / (res * res)

basepath = 'https://api.forecast.io/forecast/'+ apikey + '/' 

def getdata(i,j):
    url = basepath + str(j) + ',' + str(i) + ',' + str(unixtime)
    data = json.load(urllib2.urlopen(url))
    lat = data['latitude']
    lon = data['longitude']
    temp = data['currently']['temperature']
    humidity = data['currently']['humidity']
    pressure = data['currently']['pressure']
    clouds = data['currently']['cloudCover']
    winddir = data['currently']['windBearing']
    windspd = data['currently']['windSpeed']    
    dewpoint = data['currently']['dewPoint']
    precip = data['currently']['precipIntensity']
    f.write(('%f %f %f %f %f %f %f %f %f %f\n') % (lat, lon, temp, humidity, pressure, clouds, winddir, windspd, dewpoint, precip))
    #because output needs to be flushed...
    f.close()
#    print('%.2f%% Complete...' % (float(count / lines) * 100))

pool = Pool(processes=1)
for i in range (-180, 180, res):
    for j in range (-90, 90, res):
#        getdata(i,j)
        pool.apply_async(getdata, (i, j))
pool.close()
pool.join()


print('Total time for Execution: %f Minutes' % ((time.time() - start_time) / 60))

プロセスの数を変更すると、出力が変化し、プロセスが増えるほど、ファイルに表示されるデータが増えます。各ワーカーは別のワーカーを上書きするか、自分自身を上書きしていると思います。各ワーカーは 1 行のテキストを入力するだけでよいようです。

4

1 に答える 1

0

だから私は解決策に来ました。これらのワーカーは独自のペースでファイルに書き込むため、あまり整理されていませんが、私の目的には適しています。コールバック関数を使用してファイルに書き込み、書き込み時にプロセスが確実に完了するようにしたいと思います。これが適切な解決策だとはまだ思いませんが、うまくいきます。

def getdata(i,j):
    url = basepath + str(j) + ',' + str(i) + ',' + str(unixtime)
    data = json.load(urllib2.urlopen(url))
    lat = data['latitude']
    lon = data['longitude']
    temp = data['currently']['temperature']
    humidity = data['currently']['humidity']
    pressure = data['currently']['pressure']
    clouds = data['currently']['cloudCover']
    winddir = data['currently']['windBearing']
    windspd = data['currently']['windSpeed']    
    dewpoint = data['currently']['dewPoint']
    precip = data['currently']['precipIntensity']
    return (('%f %f %f %f %f %f %f %f %f %f\n') % (lat, lon, temp, humidity, pressure, clouds, winddir, windspd, dewpoint, precip))

def writedata(data):
    f.write(data)

pool = Pool(processes=40)
for i in range (-180, 180, res):
    for j in range (-90, 90, res):
        pool.apply_async(getdata, (i, j), callback=writedata)
pool.close()
pool.join()
f.close()   
于 2013-06-29T22:33:22.817 に答える