内部重复if__name__ =='__ main__'之外的打印语句

时间:2017-10-27 20:52:07

标签: python python-3.5 python-multiprocessing

我有一些代码正在尝试更新,而且我遇到了多次重复打印语句的问题。理想情况下,打印语句只会显示一次,以通知用户发生了什么,然后不再重复。我不知道该怎么做才能解决它,任何帮助都会非常感激!

import arcpy
import CommonDirectorySearch as cds
import multiprocessing
import os
from pathlib import Path
import tempfile
import math


# Ensure that %TEMP%\mp_mosaic directory exists since we will put output there
temp_dir = tempfile.gettempdir()
mosaic_root_dir = str(Path(temp_dir).joinpath("mp_mosaic"))
os.makedirs(mosaic_root_dir, exist_ok=True)

#sets the folder in which the rasters can be found
WSEUDir = 'E:\Models'
#Allows for counting the number of rasters in the directory
path, dirs, files = os.walk(WSEUDir).__next__()
#Counts the number of CPU cores the machine has
multiprocessing.cpu_count()
#Sets the number of CPU cores to use
#You should use no more than one less than the total number of cores!
c = multiprocessing.cpu_count()-2
#Set the number or rasters to mosaic, ideally the number of rasters to mosaic at a one time
#would be the total number of rasters divided by the number of CPU cores rounded down to the nearest integer
rm = int(math.floor(len(dirs)/c))
#rm = 2

print('Your computer has' ,multiprocessing.cpu_count(),'CPU cores, so',multiprocessing.cpu_count()-2, 'cores will be used')
print ('There are',(len(dirs)),'rasters to be processed')
print ('There wil be approximately',rm, 'rasters assigned to each core')

def mosaic2raster(input_raster_dirs):
    #prints the two rasters to be mosaiced
    print("mosaic2raster: " + input_raster_dirs)

    #sets where the new temp mosaiced rasters will be stored
    output_dir = tempfile.TemporaryDirectory(dir=mosaic_root_dir).name

    # Process: Mosaic To New Raster
    #Creates the newly mosaiced raster name
    output_raster = Path(output_dir).name

    #Does the mosaic
    arcpy.MosaicToNewRaster_management(input_raster_dirs, mosaic_root_dir,output_raster, "PROJCS['NAD_1983_StatePlane_Texas_Central_FIPS_4203_Feet',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['False_Easting',2296583.333333333],PARAMETER['False_Northing',9842500.0],PARAMETER['Central_Meridian',-100.3333333333333],PARAMETER['Standard_Parallel_1',30.11666666666667],PARAMETER['Standard_Parallel_2',31.88333333333333],PARAMETER['Latitude_Of_Origin',29.66666666666667],UNIT['Foot_US',0.3048006096012192]]", "32_BIT_FLOAT", "2", "1", "MAXIMUM", "FIRST")

    return output_dir

if __name__ == '__main__':
    #Tells where the input rasters are stored and what the raster names are
    matched, input_raster_dirs = cds.find_directory_pattern("coa_100_wseu", WSEUDir)
    if matched:
        input_rasters = ";".join(p for p in input_raster_dirs)


        # Grabbing rasters to merge
        input_rasters = []
        for i in range(0, len(input_raster_dirs), rm):
            input_rasters.append(input_raster_dirs[i] + ";" + input_raster_dirs[i+1])

        # start multiprocessing worker processes using cpu cores
        with multiprocessing.Pool(processes=c) as pool:
            temp_output_dirs = pool.map(mosaic2raster, input_rasters)
            print(temp_output_dirs)

给我:

Your computer has 12 CPU cores, so 10 cores will be used
There are 40 rasters to be processed
There wil be approximately 4 rasters assigned to each core
Your computer has 12 CPU cores, so 10 cores will be used
There are 40 rasters to be processed
There wil be approximately 4 rasters assigned to each core
mosaic2raster: E:\Models\Barton\COA\Gaines\coa_100_wseu;E:\Models\Bear\COA\BEAR\coa_100_wseu
Your computer has 12 CPU cores, so 10 cores will be used
There are 40 rasters to be processed
There wil be approximately 4 rasters assigned to each core
mosaic2raster: E:\Models\Bear\COA\BER_T2\coa_100_wseu;E:\Models\Bear\COA\BER_T2B\coa_100_wseu

0 个答案:

没有答案