如何在多处理中杀死吊工

时间:2019-03-13 14:56:40

标签: python-2.7 multiprocessing spyder

我正在使用spyder和python 2.7进行多处理。如果可迭代的长度很小(即五个元素),我有一个已经可以工作的代码。但是,当可迭代的大小很大(即120个元素)时,python多处理映射似乎会误处理某些进程。没有确定的模式,如果我重新运行代码,则会再次发生误处理,但模式不同。它的行为就好像它失去了内存中的可迭代性一样。我正在使用的部分代码如下所示。错误处理发生在方法createShapefile();

def testMethod(AREA_ID, PARENT_ID, routingPlanID, StationNameDic, message_selectStopsNoGroup, message_createShapefile, message_RA, message_VRP, message_insABPDetail, message_insertRoutingMaster, message_updateRoutingMaster, message_updRoutingPaths, message_testMethod, StationName):
    try:
        message_testMethod="OK"
        BranchIDL=[]
        branchid=""
        AREA_ID=AREA_ID.encode('utf-8')
        print "Processing: ",StationName  

        if StationName in StationNameDic:
            RouteInfo=StationNameDic[StationName]
        else:
            RouteInfo = ["ALL", "ALL", "PRESERVE_BOTH", ["OnewaySelective","Footway","Steps","Path","Tolls"], "OR", "Α,Β,Γ", "NGIS", "RA"]
        print RouteInfo
        i=0
        for item in RouteInfo:
            if i==0:
                GroupIDs=item
            elif i==1:
                BranchID=item
                if BranchID=="ALL":    
                    cn = pyodbc.connect('DRIVER={SQL Server};SERVER=dataWARE4;DATABASE=ACS_ABP;trusted_connection=yes')
                    sql = "SELECT DISTINCT BRANCH_ID FROM abp_detail with(nolock,readuncommitted) WHERE STATION_ID='"+StationName+"'and Parent_ID in ("+PARENT_ID+") and Area_ID_1='"+AREA_ID+"'"
                    print sql
                    sql=sql.decode('utf-8')
                    cur = cn.cursor()
                    cur.execute(sql)
                    branchid = cur.fetchall()
                    cn.commit()
                    #print branchid
                    for row in branchid:
                        branch_id=row[0]
                        BranchIDL.append(branch_id)
                else:
                    BranchIDL=BranchID.split(",")                                            
            elif i==2:
                EndPoint=item
            elif i==3:
                RoadRules=item
            elif i==4:
                RouteType=item
            elif i==5:
                RouteArea=item
            elif i==6:
                ACSCoords=item
            elif i==7:
                RoutingMethod=item
            i=i+1


        if AREA_ID in RouteArea:
            for branchid in BranchIDL:
                branch_id=str(branchid)
                noRouteFlag, pointGeometryList, RouteId, stationRoutes_Dic, SubRoutesList, totalStops, message_selectStopsNoGroup, routeDeliv =selectStopsNoGroup(ACSCoords, StationName, PARENT_ID, branch_id, AREA_ID, RouteType, GroupIDs, message_selectStopsNoGroup)
                if message_selectStopsNoGroup !="selectStopsNoGroup_OK":
                    continue
                if noRouteFlag==1:
                    continue
                Store_shp, message_createStore=createStore(StationName, stationRoutes_Dic, branch_id, RoutingMethod)
                if message_createStore!="createStore_OK":
                    continue
                minBin, routeStops, SortedStationStops_shp, StationDeliveries_shp, message_createShapefile = createShapefile(routeDeliv, StationName, Store_shp, pointGeometryList, branch_id, stationRoutes_Dic, PARENT_ID, AREA_ID, RouteType, SubRoutesList, message_createShapefile,StationCode)
                if message_createShapefile!="createShapefile_OK":
                    continue              
                if RoutingMethod=="VRP":
                    ExpStops_shp, groupRouteidsDic, RouteId, Route_shp, sortedExpStops_shp, Stops_shp, message_VRP = vrp(RoadRules, RouteType, StationName, Store_shp, SortedStationStops_shp, StationDeliveries_shp, minBin, RouteId, message_VRP)
                else:
                    ExpStops_shp, groupRouteidsDic, RouteId, Route_shp, sortedExpStops_shp, Stops_shp, message_RA  = ra(EndPoint, RoadRules, StationName, Store_shp, SortedStationStops_shp, StationDeliveries_shp, stationRoutes_Dic, branch_id, message_RA)
                    if message_RA!="ra_OK":
                        continue
                if  insABPDetail==1:
                    message_insABPDetail = insertABPDetail(StationName, sortedExpStops_shp, PARENT_ID, AREA_ID, message_insABPDetail)
                if updRoutingMaster==1:
                    if RouteType=="CR":
                        message_insertRoutingMaster = insertRoutingMaster(StationName, branch_id, PARENT_ID, AREA_ID, Route_shp, RouteType, message_insertRoutingMaster)
                    elif RouteType=="OR":
                        message_updateRoutingMaster = updateRoutingMaster(RouteId, routingPlanID, message_updateRoutingMaster)
                        time_elapsed7 = (time.clock() - time_start7)
                if updRoutingPaths==1:
                    message_updRoutingPaths = updateRoutingPathsAll(branch_id, groupRouteidsDic, RouteId, Route_shp, StationDeliveries_shp, StationName, Stops_shp, message_updRoutingPaths)

    except Exception as e:
        # If an error occurred, print line number and error message
        import traceback, sys
        tb = sys.exc_info()[2]
        print "An error occured on line %i" % tb.tb_lineno
        print str(e)
        message_testMethod= "An error occured on line %i" % tb.tb_lineno+" "+str(e)

   # return noRouteFlag, RouteId, stationRoutes_Dic, SubRoutesList, totalStops, message_selectStopsNoGroup, message_createShapefile, message_RA, message_VRP, message_insABPDetail, message_insertRoutingMaster, message_updateRoutingMaster, message_updRoutingPaths, message_testMethod
    return message_selectStopsNoGroup, message_createShapefile, message_RA, message_VRP, message_insABPDetail, message_insertRoutingMaster, message_updateRoutingMaster, message_updRoutingPaths, message_testMethod


if __name__ == '__main__':
    time_start = time.clock()      
    AREA_ID, PARENT_ID, routes_data, routingPlanID, StationName, stationCode_Dic = getRouteDetails(StationName, PARENT_ID, AREA_ID)
    copyDailyABPDetail(PARENT_ID, AREA_ID)
    AREA_ID=AREA_ID.decode('utf-8')
    StationsList=[]
    for rowStations in routes_data:
        StationName=rowStations[0].encode('utf-8')
        print "StationName", StationName
        StationsList.append(StationName) 
    func = partial(testMethod, AREA_ID, PARENT_ID, routingPlanID, StationNameDic,message_selectStopsNoGroup, message_createShapefile, message_RA, message_VRP, message_insABPDetail, message_insertRoutingMaster, message_updateRoutingMaster, message_updRoutingPaths, message_testMethod)

    cpuNum = multiprocessing.cpu_count()
    print "cpuNum", cpuNum 
    # Create the pool object  
    pool = multiprocessing.Pool(processes=cpuNum) 

    # Fire off list to worker function.  
    # res is a list that is created with what ever the worker function is returning  
    results=pool.map(func,StationsList, chunksize=1)        
    pool.close()  
    pool.join()  
    print results

通过反复试验,我发现如果将处理器数量设置为比最大处理器少一,将块大小设置为1,则我得到的错误最少。我的主要问题是,如果处理不当,程序将永远不会结束。我该如何在方法createShapefile()中设置一个超时值,让它说2分钟,这样,如果超过此时间,请杀死该工作线程并移至下一个可迭代的会话?

0 个答案:

没有答案