如何同时启动长时间运行的进程的多个实例,同时在python中将不同的参数传递给每个孩子

时间:2018-06-22 13:50:07

标签: python-3.6

我编写了一个脚本,将两个excel工作簿的内容相互比较,但是其中一个工作簿中有超过一千个条目,并且需要很长时间。我想制作一个启动脚本,要求用户输入要比较的文件的位置以及该脚本并行启动所需进程数时应运行的迭代次数。启动脚本需要跟踪文件的位置和下一个进程的开始位置,并将其全部传递给子进程,分离子进程,然后重新开始循环。是否可以使用for循环将变量参数传递给每个子代来启动相同的长期运行过程?

import xlwings as xw
import os, subprocess, sys, pygame
def CompareScript(SvrsFilePath,devicesFilePath,processes):
    SvrsFilePath = input("What is the full path of the Svrs file")
    devicesFilePath = input("What is the full path of the Devices file")
    processess= input("How many parrallel processess shoulld run")
    wb_1 = xw.Book(SvrsFilePath)  # Sets the Svrc computer Excel sheet to the wb_1 varialbe
    wb_2 = xw.Book(devicesFilePath)  # Sets the devices Excel workbook to wb_2. Pull devices form Kace.
    works = wb_1.sheets['Work Stations']    #Sets the Work Statinos sheet in wb_1 to works
    ser = wb_1.sheets['Servers']    #Sets the Servers sheet in wb_1 to ser
    cnf = wb_1.sheets['Computersnotfound']  #Sets the Computers not Found sheet in wb_1 to cnf. Must be created
    ws = wb_2.sheets['Worksheet']#Sets the Worksheet sheet in wb_2 to ws
    y = 2  #Creates a variable with the value of 2 to act as a counter. This is called on lines 68,69,73 and 76
    workstation_names = works.range('A2:A1087').value  #Creates a list called workstation_names out of the names of the work stations in works
    server_names = ser.range('A2:A170').value  #Creates a list called server_names out of the names of the servers in ser
    device = ws.range('A2:A590')  #Creates a list called devices out of the device names in ws
    devicelen = len(device) #Creates a varialbe equal to the length of
###_______________________________________________Compares the devices in 'devices' to those in Workstations and servers___________________________________###
    for i in range(iterations +1,int(devicelen),4):  #Sets the length of the loop starting with the first row of names and counting by 4
        x = str(i)
        #print(i)
        name = ws.range('A'+x).value #creates the name variable and sets it equal to the value in cell A'x'
        owner = ws.range('AI'+x).value #creates the owner variable and sets it equal to the value in cell AI'x'
        if ws.range('A'+x).color == (63,63,118): #checks if the computer in devices is already highlighted
            continue
        if works.range('A'+x).color !=None: #checks if the computers in workstation is already highlighted
            continue
        if ser.range('A'+x).color !=None: #checkes if the computers in servers is already highlighted
            continue
        if name in workstation_names: #looks to see if name is in the workstation list
            same = str(workstation_names.index(name))
            #print(same)
            ping = subprocess.call('ping '+name) #pings name
            #print(ping)
            name_place = workstation_names.index(name)
            if ping == 0:
                works.range('A'+same).color=(55,86,35)  #changes the cell of a sucessfull ping to dark green
            elif ping == 1:
                works.range('A'+same).color=(130,0,0) #changes the cell of a failed ping to dark red
                works.range('F'+same).value=owner #sets the value of the cell F'same' to the owner of the device
            else:
                works.range('A'+same).color=(156,87,0) #changes the cell of any other ping to yellow
                workstation_names[int(same)] = "Done"
                #print('work_station names ',workstation_names)
                #print(same)
                #print(workstation_names[int(same)])
                #print(i)
                ws.range('A'+x).color = (63,63,118) #changes the checked cell in ws to blue
        elif name in server_names: #looks to see if name is in the server list
            same = str(server_names.index(name))
            ping = subprocess.call('ping '+str(name)) #pings name
            #print(ping)
            name_place = server_names.index(name)
            if ping == 0:
                ser.range('A'+same).color = (55,86,35)  #changes the cell of a sucessfull ping to dark green
            elif ping == 1:
                ser.range('A'+same).color=(130,0,0) #changes the cell of a sucessfull ping to dark red
                ser.range('F'+same).value=owner #sets the value of cell F'same' to the owner of the device
            else:
                ser.range('A'+same).color=(156,87,0) #changes the cell of any other ping to yellow
                server_names[int(same)] = "Done"
                #print('server_names ',server_names)
                #print(same)
                #print(server_names[int(same)])
                #print(i)
                ws.range('A'+x).color = (63,63,118) #changes the checked cell in ws to blue
        else: #puts any value not in Workstations or Servers in Computers not found
            ip = (str(ws.range('B'+x).value))#gets the ip of the unknown device
            #time.sleep(5)
            cnf.range('A'+str(y)).value = name #populates the Name cell with the name of the unknown device
            cnf.range('B'+str(y)).value = ip#populates the ip cell with the ip of the unknown device
            ping = subprocess.call('ping '+name)#pings the unknown device
            #print(ping)
            if ping == 0:
                cnf.range('C'+str(y)).value = "True" #populates the ping cell with true if ping is true
                y = y+1
            else:
                cnf.range('C'+str(y)).value = "False" #populates the ping cell with false if ping is false
                y = y+1
###_______________________________________________Changes the values in the appropriate list to 'Done'_____________________________________________________###
        if name in workstation_names:
            same = str(workstation_names.index(name))
            workstation_names[int(same)] = "Done"
            #print('work_station names ',workstation_names)
            #print(same)
            #print(workstation_names[int(same)])
            #print(i)
        elif name in server_names:
            same = str(server_names.index(name))
            server_names[int(same)] = "Done"
            #print('server_names ',server_names)
            #print(same)
            #print(server_names[int(same)])
            #print(i)
            ws.range('A'+x).color = (63,63,118)
    #    if i == 538:
            #print(ping)
            #break
###_______________________________________________Pings the devices not done in the list___________________________________________________________________###
    for c in range(iterations+1,len(server_names),4): #loops through the server_names list starting with the first cell that has a name counting by 4
        if server_names[c] != "Done" and ser.range('A'+str(c)).color==None: #checks to see if the device's cell is colored and if it is 'Done' in the list
            name = str(server_names[c])
            ping = subprocess.call('ping '+name) #pings the name
            if ping == 0:
                ser.range('A'+str(c)).color=(198,239,206) #changes the cell of a sucessfull ping to light green
            elif ping == 1:
                ser.range('A'+str(c)).color=(206,62,62) #changes the cell of a failed ping to light red
            else:
                ser.range('A'+str(c)).color=(255,182,93) #changes the cell of any other ping to yellow
        #print('server working')
        else: #if the device is 'Done' or the cell is highlighted it is skipped
            # print('running')
            continue
    #print('for loop running')
    #print(c)
    for c in range(iterations,len(workstation_names),4): #loops through the workstations_names list starting with the first cell that has a name counting by 4
    #print(workstation_names[counter])
        if workstation_names[c] != "Done" and works.range('A'+str(c)).color==None: #checks to see if the device's cell is colored and if it is 'Done' in the list
            name = str(workstation_names[c])
            ping = subprocess.call('ping '+name) #pings name
            if ping == 0:
                works.range('A'+str(c)).color=(198,239,206) #changes the cell of a sucessfull ping to light green
            elif ping == 1:
                works.range('A'+str(c)).color=(255,199,206) #changes the cell of a failed ping to light red
            else:
                works.range('A'+str(c)).color=(255,182,93) #changes the cell of any other ping to yellow
        #print('Work Station working')
        else: #if the device is 'Done' or the cell is highlighted it is skipped
            #print('running')
            continue
    #print('for loop running')
    #print(c)
    print('Done') #Lets you know the it is done
    CompareScript(SvrsFilePath,devicesFilePath,iterations)

这是我为制作启动脚本而修改的版本。我可以将自己的尝试发布在会有所帮助的启动脚本上。

编辑: 我意识到,我从来没有问过一个像我希望有人为我自己写的那样使它缝起来的问题。现在的问题在第一段。

0 个答案:

没有答案