yakupme yakupme - 3 days ago 7
Python Question

Export multiple files with subprocess

I would like export multiple txt files with subprocess.

ip_address = ['172.16.{}.{}'.format(rack_number, box_number) for box_number in stb_list]

ip_address = ['10.10.8.89'] # Testing for single ip

# def planner_events_info():


#Connect to Boxes

if len(ip_address) > 0:



for ip in ip_address:


action = 'FullExport'
menu_action = 'all'
arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed'
'\\Utils\\UPNP_Client_Cmd_Line.py')]
arg_list.append(' --action=')
arg_list.append(action)
arg_list.append(' --ip=')
arg_list.append(ip)
arg_list.append(' --menu=')
arg_list.append(menu_action)

x = subprocess.Popen(arg_list, shell=True)

# print arg_list

with open("output.txt", "w+") as output:
subprocess.call(["python", arg_list], stdout=output)


With single ip address I can export output.txt. I am writing a script for maximum 16 different ips.

ip_address = ['172.16.1.1, 172.16.1.2, 172.16.1.3, 172.16.1.4, ]


For instance, for the above ip addresses, I want to export 4 txt files. Any help, will be appreciated.!

Answer

1. One after the other - serial pipeline

for i,ip in enumerate(ip_address):
    filename = "output_"+str(i)+".txt"
    #...
    with open(filename, "w+") as output:
        subprocess.call(["python", arg_list], stdout=output)

2. Parallel pipeline

Async run with apply_async from multiprocessing.Pool

This is a full running example

import multiprocessing

ip_address = ['172.16.1.1', '172.16.1.2', '172.16.1.3', '172.16.1.4', ]

def runProcess(arg_list,output_file):
    print('Will run:\n    %s\nand save to \n    %s\n'%(arg_list, output_file))
    x = subprocess.Popen(arg_list, shell=True)

    with open(output_file, "w+") as output:
        subprocess.call(["python", arg_list], stdout=output)

tasks=[]
#prepare list of tasks
for i,ip in enumerate(ip_address):
    filename = "output_"+str(i)+".txt"
    action = 'FullExport'
    menu_action = 'all'
    arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed'
                    '\\Utils\\UPNP_Client_Cmd_Line.py')]
    arg_list.append(' --action=')
    arg_list.append(action)
    arg_list.append(' --ip=')
    arg_list.append(ip)
    arg_list.append(' --menu=')
    arg_list.append(menu_action)

    tasks.append((arg_list,filename)) #pairs: (arg_list,output_file)

#run tasks
numthreads = multiprocessing.cpu_count()
pool = multiprocessing.Pool( numthreads )
results = [pool.apply_async( runProcess, t ) for t in tasks]
pool.close()
pool.join()
Comments